0

These are the photo uploading code:

$.ajax({
    url: "php/uploadPhoto.php",
    type: "POST",
    data: formdata,
    processData: false,
    contentType: false,
    beforeSend: function(XMLHttpRequest){
        XMLHttpRequest.upload.addEventListener("progress", function(evt){
            if(evt.lengthComputable){
                var percentComplete = evt.loaded / evt.total;
                alert(percentComplete);
            }
        }, false);
    },
    success: function (res) {

    }
});

It works if I do not add the "beforeSend" part....

user2018783
  • 89
  • 2
  • 7
  • If you are aiming at listening to upload progress when submitting a form with ajax, please have a look at this very useful jquery plugin: http://www.malsup.com/jquery/form/#options-object – atondelier Apr 06 '13 at 15:52
  • Probably a JavaScript error in beforeSend – scottheckel Apr 06 '13 at 15:58

1 Answers1

1

Try this:

$.ajax({
    url: "",
    data: formdata,
    cache: false,
    contentType: false,
    processData: false,
    type: 'POST',
    xhr: function() {
        var myXhr = $.ajaxSettings.xhr();
        if (myXhr.upload) {
            if (myXhr.upload.addEventListener) {
                myXhr.upload.addEventListener('progress', `YOUR CALLBACK`, false);
            } else if(myXhr.upload.attachEvent) {
                myXhr.upload.attachEvent('progress', `YOUR CALLBACK`);
            }
        }
        return myXhr;
    },
    success: function (data) { },
    error: function (jqXHR, textStatus, errorThrown) { }
});
Jason Foglia
  • 2,414
  • 3
  • 27
  • 48