如何在 IE 中使用 javascript 从客户端获取文件大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13512009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get file size from clientside using javascript in IE?
提问by raki
I used the following methode
我使用了以下方法
HTML
HTML
<input type="file" id="loadfile" />
JavaScript
JavaScript
var file = document.getElementById('loadfile').files[0];
alert( "name " + file.name + "Size " + file.size );
It works fine other browsers except IE :( How to get in IE ?
它在除 IE 之外的其他浏览器上都可以正常工作:( 如何进入 IE ?
回答by Lloyd
IE doesn't supply file size information I'm afraid. You might be able to use HTML5 File API with IE10, see here:-
IE 恐怕不提供文件大小信息。您也许可以在 IE10 中使用 HTML5 文件 API,请参见此处:-
Javascript to check filesize before upload in Internet Explorer
回答by rahul
you can do it like this using activeX
你可以使用activeX这样做
function getSize()
{
var myFSO = new ActiveXObject("Scripting.FileSystemObject");
var filepath = document.upload.file.value;
var thefile = myFSO.getFile(filepath);
var size = thefile.size;
alert(size + " bytes");
}
see here for more detail;
请参阅此处了解更多详情;
how validate file size using HTML and Javascript on client side
回答by Harish Singh
document.getElementById('loadfile').addEventListener('change', checkFile, false);
function checkFile(e) {
var file_list = e.target.files;
for (var i = 0, file; file = file_list[i]; i++) {
var fileExtension = file.name.split('.')[file.name.split('.').length - 1].toLowerCase();
var iConvert = (file.size / 1024).toFixed(2);
txt = "File type : " +fileExtension + "\n";
if(file.size > (1024 * 1024)){
txt += "Size: " + (file.size / (1024*1024)).toFixed(2) + " MB \n";
} else {
txt += "Size: " + (file.size / 1024).toFixed(2) + " KB \n";
}
alert(txt);
}
}
see filddle
看小提琴
回答by Mahmoud Farahat
IE doesn't support File API
IE 不支持文件 API
source : https://github.com/blueimp/jQuery-File-Upload/issues/147
来源:https: //github.com/blueimp/jQuery-File-Upload/issues/147
have to use an ActiveX control to perform this action
必须使用 ActiveX 控件来执行此操作
function getSize()
{
var myFSO = new ActiveXObject("Scripting.FileSystemObject");
var filepath = document.upload.file.value;
var thefile = myFSO.getFile(filepath);
var size = thefile.size;
alert(size + " bytes");
}
来源:http: //www.sencha.com/forum/showthread.php?196859-File-Upload-Field-IE-Safari-Opera-fileInput-error.&s=b124834725ae363759158268d91ac32c

