The asynchronous uploading of files in the website is a troublesome problem, but now it is easy to solve this problem by jquery.
Using the jquery2.1 version, the older version does not support the asynchronous file upload function;
Form code:
[HTML]View PlainCopyPrint?
- < form id="fileuploadform">
- < input type="file " name="file" class = "Imageupload" />
- < span id="commit"> submit to server </ Span>
- </ form >
[HTML]View PlainCopyPrint?
- < form id="fileuploadform">
- < input type="file " name="file" class = "Imageupload" />
- < span id="commit"> submit to server </ Span>
- </ form >
<form id= "Fileuploadform" > <input type= "file" name= "file" class= "Imageupload"/> <span id= " Commit "> Submit to Server </span></form>
Create a form that contains a file input
Script code:
[JavaScript]View PlainCopyPrint?
- function UploadFile () {
- //encapsulates the form as a Formdata object
- var FormData = New FormData ($ (' #fileUploadForm ') [0]);
- $.ajax ({
- URL:' http://127.0.0.1:8080/image/',
- Type:' POST ',
- //Upload the Formdata object as a parameter
- Data:formdata,
- ContentType: false,
- ProcessData: false,
- Success:function (data) {
- //File upload after successful processing
- $ (' #showUploadContent '). Append ('<div>name: '+data.name+' <br/ >url: '+data.url+' +data.url+'" >< /div> ')
- }
- })
- }
- $ (function () {
- //Add a click event for the Submit button
- $ (' #commit '). Click (function () {
- UploadFile ();
- })
- })
[JavaScript]View PlainCopyPrint?
- function UploadFile () {
- //encapsulates the form as a Formdata object
- var FormData = New FormData ($ (' #fileUploadForm ') [0]);
- $.ajax ({
- URL:' http://127.0.0.1:8080/image/',
- Type:' POST ',
- //Upload the Formdata object as a parameter
- Data:formdata,
- ContentType: false,
- ProcessData: false,
- Success:function (data) {
- //File upload after successful processing
- $ (' #showUploadContent '). Append ('<div>name: '+data.name+' <br />url: '+data.url+' +data.url+' "></div>")
- }
- })
- }
- $ (function () {
- //Add a click event for the Submit button
- $ (' #commit '). Click (function () {
- UploadFile ();
- })
- })
function UploadFile () { //encapsulates the form as a FormData object var formData = new FormData ($ (' #fileUploadForm ') [0]); $.ajax ({ URL: ' http://127.0.0.1:8080/image/', type: ' POST ', //FormData object is uploaded as a parameter data:formdata, Contenttype:false, processdata:false, success:function (data) { //File upload after successful processing $ (' # Showuploadcontent '). Append (' <div>name: ' +data.name+ ' <br/>url: ' +data.url+ ' </div> ')}} ) } $ (function () { //Add click event for Submit button $ (' #commit '). Click (function () { uploadfile (); }) })
Note: The Submit button uses <button/> <input type= "Submit"/> The case will be the page jumps, I use the <span/> element:
<span id= "Commit" > Submit to Server </span>
So that the file upload will not appear page jump, to achieve asynchronous upload;
Jquery formdata file Asynchronous Upload Quick guide