简体   繁体   English

上传问题之前检测文件大小

[英]File size detect before upload issue

Following is my code in which I am detecting filesize before upload but this document.getElementById('file').files[0] property is not supported by some browsers. 以下是我在上传之前检测文件大小的代码,但某些浏览器不支持此document.getElementById('file').files[0]属性。 All I wanna do is if on detecting browser doesnot support this property user gets alert of no else yes . 所有我想要做的是,如果在检测浏览器亘古不支持此属性用户得到的警报no其他人yes Kindly let me know how to do this? 请告诉我怎么做?

var filename = document.getElementById('file').files[0];
if (typeof filename === 'undefined' || filename === false)
{
alert('no');
}
     else if(filename && filename.size < 10240) { // 10485760 = 10 MB (this size is in bytes)
     alert('yes');
     $('#processing-modal').modal('show');
     return false;
     }

You've got it almost. 你差不多了。 Check for the property beforehand: 事先检查物业:

var element = document.getElementById('file');
if (typeof element.files !== "undefined") {
    // and now your code
}

[edit] Or as suggested here : [编辑]或按照此处的建议:

if (typeof FileReader !== "undefined") {
    // your code here
}

Indeed, IE does not support the HTML5 file API (big surprise right?) 的确,IE不支持HTML5文件API(大惊喜吧?)

we can simply check the files property of the input - and thus provides us with a workaround for crappy IE. 我们可以简单地检查输入的files属性 - 从而为我们提供了一个蹩脚的IE的解决方法。

if (!this.files) alert('message');

working example: http://jsfiddle.net/whiteb0x/nHBDp/ 工作实例: http//jsfiddle.net/whiteb0x/nHBDp/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM