2017  Kodetalk | Feedback | Privacy Policy | Terms | About

How can I upload files asynchronously?

How to upload a file asynchronously with jQuery api.
With HTML5 you CAN make file uploads with Ajax and jQuery. Not only that, you can do file validations (name, size, and MIME-type) or handle the progress event with the HTML5 progress tag (or a div). Recently I had to make a file uploader, but I didn't want to use Flash nor Iframes or plugins and after some research I came up with the solution.


<form enctype="multipart/form-data">
    <input name="file" type="file" />
    <input type="button" value="Upload" />

First, you can do some validation if you want. For example, in the onChange event of the file:

    var file = this.files[0];
    var name = file.name;
    var size = file.size;
    var type = file.type;
    //Your validation

Now the Ajax submit with the button's click:

    var formData = new FormData($('form')[0]);
        url: 'upload.php',  //Server script to process data
        type: 'POST',
        xhr: function() {  // Custom XMLHttpRequest
            var myXhr = $.ajaxSettings.xhr();
            if(myXhr.upload){ // Check if upload property exists
                myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // For handling the progress of the upload
            return myXhr;
        //Ajax events
        beforeSend: beforeSendHandler,
        success: completeHandler,
        error: errorHandler,
        // Form data
        data: formData,
        //Options to tell jQuery not to process data or worry about content-type.
        cache: false,
        contentType: false,
        processData: false

Now if you want to handle the progress.

function progressHandlingFunction(e){

Answer is