0

这些是照片上传代码:

$.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) {

    }
});

如果我不添加“beforeSend”部分,它会起作用....

4

1 回答 1

1

尝试这个:

$.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) { }
});
于 2014-01-29T16:02:51.763 回答