drn5375 2018-08-03 12:44
浏览 192

在提交之前,uploadProgress在ajax jquery中不起作用

can any one help me in this problem and i try many ways but beforeSubmit and uploadProgrss doesnt work

$.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;
    }
})
  • 写回答

1条回答 默认 最新

  • dongyi2993 2018-08-03 14:39
    关注

    Try this:

    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
                }
            });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)