[英]Checking file size during upload and stopping upload exceeding file size limit with javascript?
Is there a way to use javascript or jquery to check the progress of a file upload (ie how many bytes or kb the server has received) and to cut off the upload if it exceeds a certain limit, showing the user a warning/error message? 有没有一种方法可以使用javascript或jquery检查文件上传的进度(即服务器已接收多少字节或kb),并在超过一定限制时切断上传,向用户显示警告/错误消息? Thank you. 谢谢。
This example might help you out: http://js1.hotblocks.nl/tests/ajax/file-drag-drop.html 此示例可能会帮助您: http : //js1.hotblocks.nl/tests/ajax/file-drag-drop.html
(It also includes the drag/drop interface but that's easily ignored.) (它还包含拖放界面,但很容易忽略。)
Basically what it comes down to is this: 基本上可以归结为:
<input id=files type=file>
<script>
document.getElementById('files').addEventListener('change', function(e) {
var file = this.files[0];
var xhr = new XMLHttpRequest();
xhr.file = file; // not necessary if you create scopes like this
xhr.addEventListener('progress', function(e) {
var done = e.position || e.loaded, total = e.totalSize || e.total;
console.log('xhr progress: ' + (Math.floor(done/total*1000)/10) + '%');
}, false);
if ( xhr.upload ) {
xhr.upload.onprogress = function(e) {
var done = e.position || e.loaded, total = e.totalSize || e.total;
console.log('xhr.upload progress: ' + done + ' / ' + total + ' = ' + (Math.floor(done/total*1000)/10) + '%');
};
}
xhr.onreadystatechange = function(e) {
if ( 4 == this.readyState ) {
console.log(['xhr upload complete', e]);
}
};
xhr.open('post', url, true);
xhr.send(file);
}, false);
</script>
In the progress method you got the file size etc. I hope this solves your issue. 在进度方法中,您得到了文件大小等。我希望这可以解决您的问题。 Regards. 问候。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.