提交使用AJAX HTML表单,其中包括文件​​输入

问题描述:

你能提交通过AJAX的形式,有一个文件类型的输入?我试图使用jQuery来做到这一点,但它似乎它不能被序列化提交的文件。这是不是被阻止通过浏览器存在安全问题?有没有变通的办法?

Can you submit a form via AJAX that has a file type input? I am trying to use jquery to do this but it appears that it cannot serialize the file being submitted. Is this something that is blocked by the browser as a security concern? Is there a way around it?

您可以使用文件API ,HTML5的一部分,要做到这一点:

You can use the File API, part of HTML5, to do this:

http://www.html5rocks.com/tutorials/file/dndfiles/

有关它张贴讨论,你可以用

For a discussion on posting with it you can start with

http://groups.google.com/group/play-framework/browse_thread/thread/6223a9b25b87a839/6c74eda4f7b33010

基本上,使用文件API ,JavaScript可以读取本地系统文件,如果浏览器支持它,然后你可以发布,通过一个Ajax调用,与任何其他你需要提交。

Basically, using the File API, javascript can read files from the local system, if the browser supports it, and then you can just post that through an ajax call, along with whatever else you need to submit.

如果您需要提交多个文件,这可能是一个很好的起点:

If you need to submit multiple files, this may be a good starting point:

http://robertnyman.com/2010/04/22/using-the-file-api-for-reading-file-information-multiple-file-uploads-another-sister-specification-to-html5/

如果您必须使用jQuery,那么你可以尝试这个插件,虽然我从来没有使用过:

If you must use jQuery, then you can try this plugin, though I have never used it:

http://plugins.jquery.com/blueimp-file-upload -jquery-UI /