Jquery formdata file Asynchronous Upload Quick guide

Source: Internet
Author: User

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?
  1. < form id="fileuploadform">
  2. < input type="file " name="file" class = "Imageupload" />   
  3. < span id="commit"> submit to server </ Span>
  4. </ form >   
[HTML]View PlainCopyPrint?
  1. < form id="fileuploadform">
  2. < input type="file " name="file" class = "Imageupload" />   
  3. < span id="commit"> submit to server </ Span>
  4. </ 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?
  1. function UploadFile () {
  2. //encapsulates the form as a Formdata object   
  3. var FormData = New  FormData ($ (' #fileUploadForm ') [0]);
  4. $.ajax ({
  5. URL:' http://127.0.0.1:8080/image/',
  6. Type:' POST ',
  7. //Upload the Formdata object as a parameter   
  8. Data:formdata,
  9. ContentType: false,
  10. ProcessData: false,
  11. Success:function (data) {
  12. //File upload after successful processing   
  13. $ (' #showUploadContent '). Append ('<div>name: '+data.name+' <br/ >url: '+data.url+' +data.url+'" >< /div> ')
  14. }
  15. })
  16. }
  17. $ (function () {
  18. //Add a click event for the Submit button   
  19. $ (' #commit '). Click (function () {
  20. UploadFile ();
  21. })
  22. })
[JavaScript]View PlainCopyPrint?
  1. function UploadFile () {
  2. //encapsulates the form as a Formdata object   
  3. var FormData = New  FormData ($ (' #fileUploadForm ') [0]);
  4. $.ajax ({
  5. URL:' http://127.0.0.1:8080/image/',
  6. Type:' POST ',
  7. //Upload the Formdata object as a parameter   
  8. Data:formdata,
  9. ContentType: false,
  10. ProcessData: false,
  11. Success:function (data) {
  12. //File upload after successful processing   
  13. $ (' #showUploadContent '). Append ('<div>name: '+data.name+' <br />url: '+data.url+' +data.url+' "></div>")
  14. }
  15. })
  16. }
  17. $ (function () {
  18. //Add a click event for the Submit button   
  19. $ (' #commit '). Click (function () {
  20. UploadFile ();
  21. })
  22. })
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.