How to easily upload files without form submission (with jQuery + AJAX)

拜拜、爱过 提交于 2019-12-04 05:13:09

问题


So I have some form processing code which processes the standard text inputs and also uploaded files (through the $_FILES array)

I want to have the submission done through AJAX

Will jQuery's post(). method still pass that stuff through -> $_FILES or do I need to do something special?


回答1:


Ajax (as defined, using JavaScript) cannot perform file uploads, as JavaScript cannot access the local filesystem. There are workarounds that seem to use Ajax, such as an iframe or using Flash.

Hunt around Google, you'll find something. There are more libraries (YUI for example) that are putting this together.




回答2:


To upload files via Ajax, you need to use a workaround using iframes or Flash.

I recommend you Uploadify, it uses flash to do the uploading, and uses jQuery. I like the fact that it has many settings, and it comes with a nice default style for the queue handling. Just follow the instructions to set it up in your site.

In the PHP script you'll receive the file in the $_FILES array, just like if it were a normal submission via a form, the default name is 'Filedata', so you can access your file via $_FILES['Filedata']. Then just echo the response for the Ajax request (JSON or XML), and execute some JS code for the Uploadify 'onComplete' event.




回答3:


You can try using HTML 5's file upload API, which will require they drag and drop the file into the browser window. Read more here: http://www.appelsiini.net/2009/10/html5-drag-and-drop-multiple-file-upload

Additionally, you can do the same thing with Google Gears (which requires an install for IE but is default on Chrome).

It is possible to detect the presence of either and degrade gracefully.



来源:https://stackoverflow.com/questions/2550166/how-to-easily-upload-files-without-form-submission-with-jquery-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!