简体   繁体   English

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

[英]Ajax file upload with formData fails on large files

I'm sending via ajax a FormData object containing uploads files but when I try to upload large files (> 5M), I receive empty $_POST and $_FILES variables.我通过 ajax 发送一个包含上传文件的 FormData 对象,但是当我尝试上传大文件(> 5M)时,我收到空的 $_POST 和 $_FILES 变量。

Reading so much posts about this issue I applied almost everything but didn't work.阅读了很多关于这个问题的帖子,我几乎应用了所有内容,但没有奏效。

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

My code looks as follows:我的代码如下所示:

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,
   ...
});

So, for files < 5M everything works perfect in both my local server and production server.因此,对于小于 5M 的文件,在我的本地服务器和生产服务器中一切正常。 For files > 5M only works on my local server but not in the production server (where I receive empty $_POST and $_FILES variables).对于大于 5M 的文件,仅适用于我的本地服务器,但不适用于生产服务器(我收到空的 $_POST 和 $_FILES 变量)。 They have in general some variables configuration although production server is under a WAF on CloudFlare.尽管生产服务器位于 CloudFlare 上的 WAF 下,但它们通常具有一些变量配置。

I have the same issue on my setup.我的设置也有同样的问题。 I am not using jQuery but vanilla js.我没有使用 jQuery,而是使用 vanilla js。

The Problem is not the server!问题不是服务器! If you have a look into the developer tools of your browser you can see that the HTTP-Request has no form data.如果您查看浏览器的开发人员工具,您会发现 HTTP-Request 没有表单数据。

Small Upload <6MB is fine小上传 <6MB 没问题

小上传 <6MB 没问题

Big Upload >=6MB doesn't create FormData: Big Upload >=6MB 不会创建 FormData:

大上传 >=6MB 不会创建 FormData

My Code is:我的代码是:

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

Finally I got the reason.终于知道原因了。 Thank you to all of you and sorry for forgetting to explain what happened finally.谢谢大家,很抱歉忘记解释最后发生的事情。 It was the WAF from CloudFlare!, which limits the upload size to 5M.它是来自 CloudFlare! 的 WAF,它将上传大小限制为 5M。 Yes, that's the reason.是的,就是这个原因。 So I changed my code to make uploads divided in parts smaller than 5M and then join them again in the server once they are uploaded.所以我更改了我的代码,将上传分成小于 5M 的部分,然后在上传后再次将它们加入服务器。 I didn't find any better approach but hey, it works!.我没有找到任何更好的方法,但是嘿,它有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM