I would like block the upload files over 10mb? not about the maximum size of a single file. How to do it ? Any help and code samples would be very appreciated. Thanks!
Asked
Active
Viewed 1,013 times
0
-
do you mean 10Mb max for an upload (over 1 or more files). Or do you mean 10Mb in total, ever. For example, would I be allowed to upload 1Mb file 11 times? – Dave Becker Mar 07 '17 at 11:19
-
@DaveBecker 10mb max for an upload ( over 1 or more files) – tuszek Mar 07 '17 at 11:24
-
Does this help: http://stackoverflow.com/questions/19038390/uploading-multiple-files-mvc-is-there-a-total-file-size-limit – Dave Becker Mar 07 '17 at 11:31
1 Answers
0
Simply, there's no way to do what you want server-side. You can only limit individual file size, not the size of a full collection of files. While you could add some server-side validation to check the full size of all the files combined, by that point, the damage is already done: the server has already accepted the full upload, so you might as well do something with it, instead of simply returning an error.
HTML5, however, affords you a client-side solution, but the standard caveats apply. First, the solution will be JavaScript-based, and as with any JS, it can be disabled or tampered with. Second, it requires JS APIs only available in modern browsers, so it will be completely ineffective in IE 7-10.
$('#MyFileInput').on('change', function () {
var totalSize = 0;
for (var i = 0; i < this.files.length; i++) {
totalSize += this.files[i].size;
}
if (totalSize > 10 * 1024 * 1024) {
// present error to user
}
});

Chris Pratt
- 232,153
- 36
- 385
- 444