HTML5 jquery+formdata Asynchronous upload file with progress bar

Source: Internet
Author: User

  1. <! DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <link href=". /resources/css/common.css " rel="stylesheet " />
  6. <script src=". /resources/js/jquery-2.1.4.js "></script>
  7. </head>
  8. <body>
  9. <h2>html5 asynchronous upload file with progress bar </H2>
  10. <form method="POST" enctype="Multipart/form-data">
  11. Other information to submit:<input type="text" name="Otherinfo"/><br/> <br/>
  12. Select the file to upload:<br/>
  13.         < Input type= "file"  name = "file"  />< span></span ><br/>  
  14. <input type="file" name="file" /><span></span ><br/>
  15. </form>
  16. <br/><br/>
  17. <input type="button" value= "upload Bar" onclick="upload ()"/>
  18. <br/><br/>
  19. Upload progress:<progress></Progress><br/>
  20. <p id="Progress">0 bytes</P>
  21. <p id="info"></P>
  22. </body>
  23. <script>
  24. var totalsize = 0;
  25. A handler for the onchange event that binds all type=file elements
  26. $ (': File '). Change (function () {
  27. var file = This.files[0]; //Assuming that the file tag does not have the Multiple property open, then just take the first one.
  28. name = File.name;
  29. size = File.size;
  30. Type = File.type;
  31. url = window. Url.createobjecturl (file); //Get the URL of the local file, if it is a picture file, can be used to preview the picture
  32. $ (this). Next (). HTML ("FileName:" + name + "file type:" + type + " File Size:" + size + "url:" + URL);
  33. TotalSize + = size;
  34. $ ("#info"). HTML ("Total size:" + totalsize + "bytes");
  35. });
  36. function upload () {
  37. Creates a Formdata object that is initialized to the data in the form form. You need to add additional data to use Formdata.append ("property", "value");
  38. var formData = New FormData ($ (' form ') [0]);
  39. Ajax asynchronous uploads
  40. $.ajax ({
  41. URL: "Http://localhost:8080/MyJavaStudio/servlet/file/upload",
  42. Type: "POST",
  43. Data:formdata,
  44. XHR: function () { //Gets the XHR object in Ajaxsettings, binds the handler for the Progress event for its upload property
  45. MYXHR = $.AJAXSETTINGS.XHR ();
  46. if (myxhr.upload) { //Check if upload property exists
  47. callback function for binding progress event
  48. MyXhr.upload.addEventListener (' progress ', progresshandlingfunction, false);
  49. }
  50. return MYXHR; //XHR object returned to jquery using
  51. },
  52. Success: function (Result) {
  53. $ ("#result"). HTML (result.data);
  54. },
  55. ContentType: false, //must be false to automatically add the correct content-type
  56. ProcessData: false //must be false to avoid jquery's default handling of Formdata
  57. });
  58. }
  59. Upload Progress callback function:
  60. function Progresshandlingfunction (e) {
  61. if (e.lengthcomputable) {
  62. $ (' progress '). attr ({value:e.loaded, max:e.total}); //Update data to progress bar
  63. var percent = e.loaded/e.total*100;
  64. $ (' #progress '). HTML (e.loaded + "/" + e.total+"bytes." + percent.tofixed (2) + "%");
  65. }
  66. }
  67. </script>
  68. </html>

HTML5 jquery+formdata Asynchronous upload file with progress bar

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.