2013-02-07 69 views
0

我想用jquery構建一個簡單的文件上傳器。我在沒有Symfony2的服務器上測試過它,它工作正常。Symfony 2 jquery表格

我用兩個簡單的控制器:

/** 
* @Route("/") 
* @Template() 
*/ 
public function indexAction() 
{ 
    return array(); 
} 

/** 
* @Route("/new_project", name="newproject") 
* @Method("POST") 
*/ 
public function newProjectAction() 
{ 
    $entity = new test(); 
    $entity->setName('test'); 

    $em = $this->getDoctrine()->getManager(); 
    $em->persist($entity); 
    $em->flush(); 


    return new Response('<html><body>added</body></html>'); 
} 

我的索引視圖

{% extends '::base.html.twig' %} 
{% block script %} 
<script type="text/javascript"> 
    <!-- 
    $(document).ready(function() { 


     var options = { 
     target: '#message', //Div tag where content info will be loaded in 
     url: {{ path('newproject') }}, //The php file that handles the file that is uploaded 
     beforeSubmit: function() { 
      $('#uploader').html('<img src="ajax-loader.gif" border="0" />'); //Including a preloader, it loads into the div tag with id uploader 
     }, 
     success: function() { 
      //Here code can be included that needs to be performed if Ajax request was successful 
      $('#uploader').html('Plik przesłany'); 

     } 
     }; 

     $('#upload').submit(function() { 
      $(this).ajaxSubmit(options); 
      return false; 
     }); 

    }); 
//--> 
</script> 
{% endblock %} 



{% block body %} 
<div id="message"></div> 
<form name="upload" id="upload" action="#" method="POST" enctype="multipart/form-data"> 
    <table cellpadding="4" cellspacing="4" border="0"> 
     <tr> 
      <td colspan="2"><h1>Upload File via Ajax</h1></td> 
     </tr> 
     <tr> 
      <td nowrap>File:</td> 
      <td nowrap><input type="file" name="fileToUpload" id="fileToUpload" /></td> 
     </tr> 
     <tr> 
      <td nowrap colspan="2"><input type="submit" id="uploadFile" value="Upload File" /></td> 
     </tr> 

    </table> 
</form> 
<div id="uploader"></div> 
{% endblock %} 

你能告訴我,爲什麼它不工作?

+0

運行它時是否有JavaScript錯誤?呈現的HTML如何看起來像? – cheesemacfly

回答

0
<script type="text/javascript"> 
    <!-- 
    $(document).ready(function() { 

將其更改爲

<script type="text/javascript"> 
    //<!-- 
    $(document).ready(function() { 

,並請儘量顯示錯誤和提ú[R使用也確保jQuery庫包含在頁面哪個瀏覽器。