[英]Request Entity Too Large when data sent using AJAX
I am using the JavaScript canvas API toDataURL()
to convert an image file to a base64 string and then transferring the string to my server via an XMLHttpRequest or simply an AJAX request.我正在使用 JavaScript 画布 API
toDataURL()
将图像文件转换为 base64 字符串,然后通过 XMLHttpRequest 或简单的 AJAX 请求将字符串传输到我的服务器。 When I upload medium size files(500KB-1MB) I am getting the following error-当我上传中等大小的文件(500KB-1MB)时,出现以下错误-
413 Request Entity Too Large
I have researched a lot.我研究了很多。 Tried changing
post_max_size
, upload_max_size
but that didn't help.尝试更改
post_max_size
、 upload_max_size
但这没有帮助。 In an article it was suggested to set limit of the RequestBodyLength in Apache.在一篇文章中,建议在 Apache 中设置 RequestBodyLength 的限制。 But I couldn't find a way to do that!
但我找不到办法做到这一点!
Then I made a small hack and tried to upload the file by the old school method(iframe form upload) and it worked!然后我做了一个小黑客并尝试通过旧学校方法(iframe表单上传)上传文件并且它起作用了! Any help or suggestion on how to upload using the AJAX base64 string method would be awesome.
关于如何使用 AJAX base64 字符串方法上传的任何帮助或建议都会很棒。
EDIT- Here's the JavaScript codes that first converts the canvas element to base64 string and then uploads it to the server using AJAX.编辑 - 这是首先将画布元素转换为 base64 字符串然后使用 AJAX 将其上传到服务器的 JavaScript 代码。
lab.newPost.submit = function(){
var url = 'upload.php';
var params = {
'title': lab.newPost.elements.photo_form.title.val(),
'imageData': Meme.canvas[0].toDataURL("image/jpeg"), //Convert to Base64 data URL
};
$.post(url,params, function(data){ //AJAX POST
data = JSON.parse(data);
if(data.success){
Msg.success('<a href="#" class="alert-link">Awesome! </a> Thanks for contributing :)');
}
else{
Msg.danger('<a href="#" class="alert-link">Error! </a>'+data.error[0]);
}
}).fail(function(xhr, ajaxOptions, thrownError) { //any errors?
console.log(thrownError); //This line give
});
}
The call returns the following lines-调用返回以下几行-
Request Entity Too Large
The requested resource
/upload.php
does not allow request data with POST requests, or the amount of data provided inthe request exceeds the capacity limit.
A 413 Request Entity Too Large
is normally generated when the client sends a POST request that is larger than what the server is capable of handling.当客户端发送大于服务器能够处理的 POST 请求时,通常会生成
413 Request Entity Too Large
。 This can be dependent on physical resources, or settings at different levels.这可能取决于物理资源或不同级别的设置。
The limit in Apache is set via the LimitRequestBody directive and defaults to 0: Apache 中的限制是通过 LimitRequestBody 指令设置的,默认为 0:
This directive specifies the number of bytes from 0 (meaning unlimited) to 2147483647 (2GB) that are allowed in a request body.该指令指定请求正文中允许的从 0(表示无限制)到 2147483647 (2GB) 的字节数。
Take a look at the full description of Apache'sLimitRequestBody directive.查看 Apache 的LimitRequestBody 指令的完整描述。
In PHP, different limits are taking into account: post_max_size
, upload_max_filesize
(if it is a file upload) and memory_limit
.在 PHP 中,考虑了不同的限制:
post_max_size
、 upload_max_filesize
(如果是文件上传)和memory_limit
。 upload_max_filesize
has to be lower than post_max_size
, which has to be lower than memory_limit
. upload_max_filesize
必须低于post_max_size
,后者必须低于memory_limit
。
If PHP is running on Suhosin's protection, more configuration details can cause a server to reject a POST due to its size:如果 PHP 在 Suhosin 的保护下运行,更多的配置细节可能会导致服务器由于其大小而拒绝 POST:
;suhosin.post.max_array_depth = 100
;suhosin.post.max_array_index_length = 64
;suhosin.post.max_name_length = 64
;suhosin.post.max_totalname_length = 256
;suhosin.post.max_value_length = 1000000
;suhosin.post.max_vars = 1000
Note that max_value_length here is exactly in the middle between the uploaded file size and the base64 version.注意这里的 max_value_length 正好在上传文件大小和 base64 版本之间。 So this might be the problem.
所以这可能是问题所在。 Note that in configuration, the same lines appear for suhosin.get and suhosin.request.
请注意,在配置中,suhosin.get 和 suhosin.request 出现相同的行。 suhosin.request values should be equal or higher than get and post.
suhosin.request 值应该等于或高于 get 和 post。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.