0

我正在尝试使用 jquery 构建一个简单的文件上传器。我在没有 Symfony2 的服务器上对其进行了测试,它可以工作。

我使用两个简单的控制器:

 /**
 * @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 %}

你能告诉我为什么它不起作用吗?

4

1 回答 1

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

将其更改为

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

请尝试显示错误并提及您使用的浏览器,并确保页面中包含 jquery 库。

于 2013-03-18T15:11:46.023 回答