【发布时间】:2018-09-24 04:51:26
【问题描述】:
我有一个用户可以上传多个文件的表单。
但我只想在所有文件的组合大小不超过 3GB 时允许他们这样做。我该怎么做?
这是我当前的代码:
var fileCount = 0;
var showFileCount = function() {
$('#file_count').text('# Files selected: ' + fileCount);
};
showFileCount();
$(document).on('click', '.close', function() {
$(this).parents('span').remove();
fileCount -= 1;
showFileCount();
})
$('#uploadFile').on('change', function() {
var filename = this.value;
var lastIndex = filename.lastIndexOf("\\");
if (lastIndex >= 0) {
filename = filename.substring(lastIndex + 1);
}
var files = $('#uploadFile')[0].files;
for (var i = 0; i < files.length; i++) {
var fileSize = (files[i].size / 1024 / 1024).toFixed(2);
if (files[i].size > 3204448256) {
// $("#input-file-1").html(file_names);
alert("You have exceeded maximum allowed size 3GB")
return true;
}
$("#upload_prev").append('<span>' + '<div class="filenameupload">' + files[i].name + ' (' + fileSize + ' MB)</div>' + '<p class="close" >X</p></span>');
}
fileCount += files.length;
showFileCount();
});
.filenameupload {
width: 98%;
}
#upload_prev {
border: thin solid #000;
width: 65%;
padding: 0.5em 1em 1.5em 1em;
}
#upload_prev span {
display: flex;
padding: 0 5px;
font-size: 12px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="file_count"></div>
<input type="file" id="uploadFile" name="FileUpload" multiple="multiple" />
<div id="upload_prev"></div>
我有一个用户可以上传多个文件的表单。
但我只想在所有文件的组合大小不超过 3GB 时允许他们这样做。我该怎么做?
【问题讨论】:
-
我在您的问题中没有看到一个问号。
-
@TheIncorrigible 这不是重复的。他在问如何检查所有文件的组合大小是否超过3GB
-
@blex,感谢您理解我的问题。,我已将代码添加到小提琴中进行检查
-
@Test 您的代码似乎按预期工作。你有什么问题?
标签: javascript jquery file file-upload jquery-file-upload