Upload image using AJAX with progress bar

I am trying to upload an image using AJAX. I have a local URL for my image and I want to transfer this image as a web service file for upload.

Suppose I have a local file URL: file: ///accounts/1000/shared/camera/IMG_00000322.jpg

Now, using AJAX, I want to pass this to the webservice. What would be the best way to do this? I also want to show download progress

Using php on the server side.

uploadImage : function(myImageUrl,chatId){

    var formData = new FormData();
    formData.append("chatId", chatId);
    formData.append("fileimage", myImageUrl);

        $.ajax(
        {
            type:"POST",
            url:"http://suresh.sp.in/butler/public/uploadimage/getimage",
            contentType:"image/png",
            dataType:"json",
            data:formData,
            success:function(uploaded){
                console.info(uploaded.status);
            },
            error:function(error){
                                    console.info(error);    

            }
        });

}
+4
source share
1 answer

I used this snippet on several of my sites, it handles loading, dragging and dropping Multiples files and a progress bar.

HTML

, #output .

JS

dataTransfer jQuery drop.

$(document).ready(function(){
    // We add the dataTransfer special property to our jQuery event
    $.event.props.push("dataTransfer");

    // We bind events for drag and drop
    $('#output').bind({
        "dragenter dragexit dragover" : do_nothing,
        drop : drop
    }); 

});
// stop event propagation
function do_nothing(evt){
    evt.stopPropagation();
    evt.preventDefault();
}

// Progress bar update function
function update_progress(evt,fic) {

    var id_tmp=fic.size;
    //id_tmp help us keep track of which file is uploaded
    //right now it uses the filesize as an ID: script will break if 2 files have the     
    // same size
    if (evt.lengthComputable) {
        var percentLoaded = Math.round((evt.loaded / evt.total) * 100);
        if (percentLoaded <= 100) {
            $('#'+id_tmp+' .percent').css('width', percentLoaded + '%');
            $('#'+id_tmp+' .percent').html(percentLoaded + '%');
        }
    }
}

, ​​ drop

function drop(evt){
    do_nothing(evt);

    var files = evt.dataTransfer.files;

    // Checking if there are files
    if(files.length>0){
        for(var i in files){
            // if its really a file
            if(files[i].size!=undefined) {

                var fic=files[i];

                // We add a progress listener
                xhr = jQuery.ajaxSettings.xhr();
                if(xhr.upload){
                    xhr.upload.addEventListener('progress', function (e) {
                        update_progress(e,fic);
                    },false);
                }
                provider=function(){ return xhr; };

                // We build our FormData object
                var fd=new FormData;
                fd.append('fic',fic);

                // For each file we build and Ajax request
                $.ajax({
                    url:'/path/to/save_fic.php',
                    type: 'POST',
                    data: fd,
                    xhr:provider,
                    processData:false,
                    contentType:false,
                    complete:function(data){ 
                        //on complete we set the progress to 100%
                        $('#'+data.responseText+' .percent').css('width', '100%');
                        $('#'+data.responseText+' .percent').html('100%');
                    }
                });


                // for each file we setup a progress bar
                var id_tmp=fic.size;
                $('#output').after('<div class="progress_bar loading" id="'+id_tmp+'"><div class="percent">0%</div></div>');
                $('#output').addClass('output_on');

                // We add our file to the list
                $('#output-listing').append('<li>'+files[i].name+'</li>');

            }
        }
    }

}

IE9 . , ! ( )

XMLHttpRequest

datatransfer

:

PHP

, $_FILES ..... 100% , php script .

+1

Source: https://habr.com/ru/post/1530177/


All Articles