[英]Request Entity Too Large when data sent using AJAX
我正在使用 JavaScript 畫布 API toDataURL()
將圖像文件轉換為 base64 字符串,然后通過 XMLHttpRequest 或簡單的 AJAX 請求將字符串傳輸到我的服務器。 當我上傳中等大小的文件(500KB-1MB)時,出現以下錯誤-
413 Request Entity Too Large
我研究了很多。 嘗試更改post_max_size
、 upload_max_size
但這沒有幫助。 在一篇文章中,建議在 Apache 中設置 RequestBodyLength 的限制。 但我找不到辦法做到這一點!
然后我做了一個小黑客並嘗試通過舊學校方法(iframe表單上傳)上傳文件並且它起作用了! 關於如何使用 AJAX base64 字符串方法上傳的任何幫助或建議都會很棒。
編輯 - 這是首先將畫布元素轉換為 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
});
}
調用返回以下幾行-
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.
當客戶端發送大於服務器能夠處理的 POST 請求時,通常會生成413 Request Entity Too Large
。 這可能取決於物理資源或不同級別的設置。
Apache 中的限制是通過 LimitRequestBody 指令設置的,默認為 0:
該指令指定請求正文中允許的從 0(表示無限制)到 2147483647 (2GB) 的字節數。
查看 Apache 的LimitRequestBody 指令的完整描述。
在 PHP 中,考慮了不同的限制: post_max_size
、 upload_max_filesize
(如果是文件上傳)和memory_limit
。 upload_max_filesize
必須低於post_max_size
,后者必須低於memory_limit
。
如果 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
注意這里的 max_value_length 正好在上傳文件大小和 base64 版本之間。 所以這可能是問題所在。 請注意,在配置中,suhosin.get 和 suhosin.request 出現相同的行。 suhosin.request 值應該等於或高於 get 和 post。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.