大文件上传通过ajax调用失败

时间:2016-08-06 04:45:49

标签: javascript ajax

现在我正在处理文件上传。所以我用ajax调用编写了一段代码。当我从本地服务器上载文件(GB)时,这非常有效。

$.ajax({
    type: "POST",
    url: "process/uploadFlatFile.htm",
    enctype : 'multipart/form-data',
    //timeout: 0,
    crossDomain: true,
    data : formData,
    processData: false, 
    contentType: false,
    cache: false,
    dataType: 'json',
    xhr: function() {
        myXhr = $.ajaxSettings.xhr();
        //myXhr = new XMLHttpRequest();
        console.log("myXhr: " + myXhr.upload);
        if(myXhr.upload){
            console.log("adding progress event listener");
            myXhr.upload.addEventListener('progress', showProgress, false);
        } else {
            console.log("Upload progress is not supported.");
        }
        return myXhr;
    },beforeSend: function(xhr, opts) {
        console.log("beforeSend:xhr: " + xhr + ", opts: " + opts);
        currXhr = xhr;
        showProgressBar();
        status.empty();
        var percentVal = '0%';
        bar.width(percentVal);
        percent.html(percentVal);
        $(cancelSelector).removeAttr('disabled');
    },

    success: function(result,status,xhr) {
        console.log("success:result: " + result);
        console.log("success:status: " + status);
        console.log("success:xhr: " + xhr);
        var percentVal = '100%';
        bar.width(percentVal);
        //$("#fountainGG").hide();
        percent.html(percentVal);
        $.unblockUI({ 
            onUnblock: function(){  
                if(result=="success"){
                    console.log("SUCCESS PART :")

                    alertMessage("Success","File Uploaded Successfully","info");
                    setTimeout(function(){
                        window.location.href = "processFlow.htm";
                        //newProcessClicked('yes'); closeConDiv()
                    }, 2000);
                }else{

                    alertMessage("Error","Error in upload. Try Again !","info");
                }
            } 
        });
    },
    complete: function(xhr,status){
        console.log("COMPLETE PART :")
        console.log("complete:status: " + status + ", xhr: " + xhr);
        $('#btnGoUpload').prop('disabled', false);
    },
    error: function(xhr, statusText, err) {
        $.unblockUI();
        console.log("statusText: " +statusText);
        console.log("error: " + err);
        var msg = "Error in uploading file. Try Again !";
        if (err == "abort"){
            msg = "File upload aborted.";
        }
        alertMessage("Error",msg,"info");
    }
});

但是,当我从公共服务器上传GB(30 Gb以上)文件时,它会在几次后出现错误的ajax调用部分。我认为是连接超时问题?如果是连接超时问题,我该如何解决?

1 个答案:

答案 0 :(得分:0)

您可以使用此命令解决可能的超时限制,将其放在php接收器脚本的开头:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
       <div  id="one" class="active"></div>
       <div  id="two"></div>
       <div  id="three"></div>
    </body>

要增加最大上传大小,也可以阻止你,你需要在php.ini中设置upload_max_filesize和post_max_size的值:

ini_set('max_execution_time', 0);

编辑并重新启动您的网络服务器。