首页 > 解决方案 > 在提交和uploadProgress 之前在ajax jquery 中不起作用

问题描述

谁能帮我解决这个问题,我尝试了很多方法,但是 beforeSubmit 和 uploadProgrss 不起作用

$.ajax({
    url:"include/ajaxPages/admin/insertNewItem.php",
    type:"POST",
    data:new FormData(this),
    processData:false,
    contentType:false,
    beforeSubmit:function(){
        $(this).find(".progress").width("0%");
    },
    uploadProgress:function(event,position,total,complete){
        $(this).find(".progress").animate({
            width: complete+"%"
        },{
            duration:1000 
        })
    },
    success:function(data){
        console.log(data);
        alert("تم الاضافه بنجاح");
        $(".loadingViedo").fadeOut();
        window.location = document.URL;
    }
})

标签: phpjqueryajax

解决方案


尝试这个:

var fd = new FormData();
fd.append('rName', $('#upload-resource .upload').attr('data-name'));
fd.append('rfile', $('#upload-resource .upload').attr('data-file'));

$.ajax({
            url: 'include/ajaxPages/admin/insertNewItem.php',
            data: fd,
            processData: false,
            contentType: false,
            type: 'POST',
            xhr: function () {  // custom xhr
                myXhr = $.ajaxSettings.xhr();
                if (myXhr.upload) { // if upload property exists
                    myXhr.upload.addEventListener('progress', function (e) {
                        var done = e.position || e.loaded
                        var total = e.totalSize || e.total;
                        var percent = Math.round(done / total * 100);
                        //console.log('xhr progress: ' + percent + '%');

                        $(this).find(".progress").animate({
                        width: percent +"%"
                           },{
                              duration:1000 
                           });
                    }, false);

                    myXhr.addEventListener('load', function (e) {
                        //upload done
                    });


                    $('#upload-resource .cancel').show().click(function () {
                        //cancel upload
                        myXhr.abort();
                    });
                }
                return myXhr;
            },
            success: function (data) {
                //to do for success result
            }
        });

推荐阅读