[英]Symfony2 form file upload always null
I'm having trouble uploading a file, when I dump the data returned by $form->getData(), the file field is always null.我在上传文件时遇到问题,当我转储 $form->getData() 返回的数据时,文件字段始终为空。 Also, if I do $this->getRequest()->files->all() I get an empty array().另外,如果我执行 $this->getRequest()->files->all() 我得到一个空数组()。
This is the field definition in my form type:这是我的表单类型中的字段定义:
->add('card_file', 'file', array(
'label' => 'Examinar',
'required' => false,
)
)
And this is how I print the field in the .twig file:这就是我在 .twig 文件中打印字段的方式:
<fieldset class="fieldset__upload">
{{ form_widget(addressForm.card_file) }}
</fieldset>
The tag is created with enctype="multipart/form-data", also, the form isValid() and the file is about 4kb in size, so I don't think is a problem of exceeding file size.标签是用enctype="multipart/form-data"创建的,而且形式是isValid(),文件大小大约4kb,所以我认为不是超过文件大小的问题。
The form is not attached to an entity, and $form->getData() returns all other fields correctly.表单未附加到实体,并且 $form->getData() 正确返回所有其他字段。
Any ideas?有任何想法吗? Thank you in advance先感谢您
I think I found the problem, it didn't had anything to do with Symfony2.我想我发现了问题,它与 Symfony2 没有任何关系。
This form is submitted via AJAX, and I didn't know until now that serialize() doesn't send file data.这个表单是通过 AJAX 提交的,直到现在我才知道 serialize() 不发送文件数据。
That was the problem altogether, I will update the answer with the correct JavaScript code as soon as I can get it to work.这就是问题所在,我会尽快用正确的 JavaScript 代码更新答案。
EDIT:编辑:
I used the jQuery Form plugin for AJAX for uploading the file:我使用了 AJAX 的 jQuery Form 插件来上传文件:
http://www.malsup.com/jquery/form/ http://www.malsup.com/jquery/form/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.