我有一个网络应用程序,允许用户添加多个图像,然后在按下“保存”按钮时将它们与其他数据一起上传。我试图在发送之前计算 POST 数据的总大小,以便我可以将请求分解为多个请求,以避免达到 PHP 的 post_max_size
或 max_file_uploads
错误。
目前,我使用此代码通过 HTML5 文件 API(size
属性)仅检查图像的整体大小:
// Calculate size of all files being submitted
var ttlSize = 0;
$('form').find('input[type=file]').each(function() {
ttlSize += this.files[0].size;
});
if (ttlSize > 4 * 1048576) { // >4MB
// this will exceed post_max_size PHP setting, now handle...
} else if ($('form').find('input[type=file]').length >= 10) {
// this will exceed max_file_uploads PHP setting, now handle...
}
我希望通过包含其余的 POST 数据来更加精确。这可能吗?
最佳答案
您可以通过计算序列化表单的长度(不包括文件上传字段)来估计它。所以它应该是这样的:
$("form").not("[type='file']").serialize().length
;
关于jquery - 在使用 jQuery 提交之前如何检查 POST 数据大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13420266/