Ajax+h5 upload file + progress bar

Source: Internet
Author: User

1. Replace form form to use new formdata for transmission

2. Upload file control on the page:

<input type= "File" id= "file" class= "Mr10 ml10 input-medium" >
3.ajax Implementation upload:

function Uploadsaveprojects (obj) {
    var fileobj = document.getElementById ("file"). Files[0];//Get File Object
    var Filecontroller = BaseURL + "Project/upload";                    Receive the uploaded file background address 
    //FormData object
    var form = new FormData ();
    Form.append ("Author", "Hooyes");                        Can increase the form data
    form.append ("file", fileobj);                           File Object
    //XMLHttpRequest object
    var xhr = new XMLHttpRequest ();
    Xhr.open ("Post", Filecontroller, true);
    Xhr.onload = function () {
        alert ("Upload complete!");
    Implement progress bar function
    //Xhr.upload.addEventListener ("Progress", progressfunction, false);
    Xhr.send (form);
}

4. Background can be used in any language to upload, add a progress bar, using the H5 tag

<progress  id= "ProgressBar" value= "0" max= "M" ></progress>
<span id= "Percentage" ></ Span>

5. Increase control of the progress bar JS Event

function Progressfunction (evt) {
    var ProgressBar = document.getElementById ("ProgressBar");
    var percentagediv = document.getElementById ("percentage");
    if (evt.lengthcomputable) {
        Progressbar.max = evt.total;
        Progressbar.value = evt.loaded;
        percentagediv.innerhtml = Math.Round (evt.loaded/evt.total *) + "%";
    }
}

6.formData What the hell is this thing?

XMLHttpRequest Level 2 Adds a new interface formdata. Using the Formdata object, we can simulate a series of form controls using JavaScript with some key-value pairs, and we can also use the XMLHttpRequest send () method to commit the "form" asynchronously. The great advantage of using formdata is that we can upload a binary file asynchronously compared to ordinary Ajax.

Newer versions of all major browsers already support this object, such as Chrome 7+, Firefox 4+, IE 10+, Opera 12+, Safari 5+.

Detailed Introduction: http://www.cnblogs.com/lhb25/p/html5-formdata-tutorials.html


Related Article

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.