Transferred from: http://www.cnblogs.com/labnizejuly/p/5588444.html
FormData
object, you can use a series of key-value pairs to simulate a complete form, and then use the XMLHttpRequest
"form" to send it.
<formID= "Uploadform"enctype= "Multipart/form-data"> <inputID= "File"type= "File"name= "File"/> <ButtonID= "Upload"type= "button">Upload</Button></form>
$.ajax ({ '/upload ', ' POST ', false, New FormData ($ (' #uploadForm ') [0]), false, false}) . Done (function(res) {}). Fail (function
Here are a few things to note:
processData
Set to false
. Because the data
value is an FormData
object, you do not need to process the data.
<form>
tag to add a enctype="multipart/form-data"
property.
cache
Set to false
, the upload file does not need to be cached.
contentType
Set to false
. This is set to false because it is an <form>
object constructed by a form, and the FormData
property has already been declared enctype="multipart/form-data"
.
After uploading, the server-side code needs to use the file
get file input stream object from the query parameter name, because <input>
it is declared in name="file"
.
Uploading files using Formdata objects via jquery Ajax