用formData上传Ajax文件在大文件上失败

问题描述 投票:0回答:2

我通过ajax发送包含上传文件的FormData对象,但是当我尝试上传大文件(> 5M)时,我收到空的$ _POST和$ _FILES变量。

阅读了很多有关此问题的文章,我几乎应用了所有内容,但没有用。

I've tried on php.ini:
- max_execution_time = 300
- max_input_time = 300
- post_max_size = 2048M (large but compulsory)
- upload_max_filesize = 2048M (large but compulsory)
- max_input_vars = 2000 (trying different values)

On HTTP/HTTPS directives (some have no sense but I was trying in a desperately way to see the behaviour):
- FcgidConnectTimeout 300
- FcgidIOTimeout 300
- FcgidIdleTimeout 300
- FcgidBusyTimeout 300
- IPCCommTimeout 9999
- FcgidMaxRequestLen 21474836480

我的代码如下:

var form = $('form[name="myform"]');
var formData = new FormData(form[0]);

/*
Also tried this:
   var file = $("form[name='myform'] input[name='filename']");
   formData.append("my_upload", (file[0]).files[0]);
*/

$.ajax({
   type: 'post',
   data: formData,
   dataType: 'json',
   async: true,
   url: '/mysite/controllers/Controller.php',
   processData: false,
   contentType: false,
   ...
});

因此,对于<5M的文件,无论在本地服务器还是生产服务器中,一切正常。对于大于5M的文件,仅在本地服务器上有效,而在生产服务器上则无效(我在其中收到空的$ _POST和$ _FILES变量)。尽管生产服务器在CloudFlare的WAF下,但它们通常具有一些变量配置。

javascript jquery ajax upload form-data
2个回答
0
投票

我在设置上遇到了同样的问题。我不是使用jQuery,而是使用香草js。

问题不是服务器!如果查看浏览器的开发人员工具,则可以看到HTTP-Request没有表单数据。

小上传<6MB可以

Small Upload <6MB is fine

大上载​​> = 6MB不会创建FormData:

Big Upload >=6MB doesn't create FormData

我的代码是:

function uploadFile(file, filename, domDirectory, domTabelle, domSchluessel){
var fd = new FormData();
fd.append("filename", filename);
fd.append("name", filename);
fd.append("right_id", domDirectory);
fd.append("tabelle", domTabelle);
fd.append("schluessel", domSchluessel);
var xhr = new XMLHttpRequest();
var reader  = new FileReader();
reader.addEventListener("load", function () {
    fd.append("dataURI", reader.result);
    //console.log(reader.result);
    xhr.send(fd);
  }, false
);
reader.readAsDataURL(file);
xhr.open('POST', '../control/Upload.php', true);
}

0
投票

最后我明白了原因。谢谢大家,对不起忘记解释最终发生的事情。这是CloudFlare!的WAF,它将上传大小限制为5M。是的,这就是原因。因此,我更改了代码,以使上传内容分成小于5M的部分,然后在上传后再次将其加入服务器。我没有找到更好的方法,但嘿,它有效!。

© www.soinside.com 2019 - 2024. All rights reserved.