【问题标题】:Check for file size with JavaScript before uploading in IE9 Browser在 IE9 浏览器中上传之前使用 JavaScript 检查文件大小
【发布时间】:2015-12-29 07:53:13
【问题描述】:

以下是我的 ASP.NET MVC 的一部分:当用户尝试在 IE 9 浏览器中上传文件时,它会在 this.files[0].size 这一行抛出错误; 因为 IE 9 浏览器确实支持 html 5 api(FileReader)

<input type="file" name="FileToUpload" id="FileToUpload"  style="width: 100%;" />

<!-- begin snippet: js hide: false -->

 $('#FileToUpload').bind('change', function () {
            
               var sizeInMB = '';
              
                   /* Other */
                      this.files[0].size gets the size of your file.
                   var sizeInBytes = this.files[0].size;
                      var sizeInMB = (sizeInBytes / (1024 * 1024)).toFixed(2);    2000
                    sizeInMB = $([sizeInBytes]).map(function () { return (this / 1024 / 1024).toFixed(2) })[0];
              
          
              
        });

所以当我执行这一行时,我碰巧使用了这个 sn-p $("#FileToUpload")[0].value 我没有得到文件路径我得到这样的 (C:\fakepath\MORETHAN2gbticket.zip)

有没有其他方法可以获取 IE 9 浏览器的文件大小。一种方法是可以使用 Flash。然后我需要使用类似插件(uploadify Upload jQuery) 有没有其他我可以在不使用插件的情况下获得文件大小 谁能指导我。

    var file = $("#FileToUpload");
           var objFSO = new ActiveXObject("Scripting.FileSystemObject");
           var filePath = $("#FileToUpload")[0].value;
            var objFile = objFSO.getFile(filePath);
            var fileSize = objFile.size; //size in kb

【问题讨论】:

    标签: jquery asp.net-mvc html flash internet-explorer


    【解决方案1】:

    不! IE 版本 9 没有 FileReader api,而是一个名为 File Api Lab 的原型:

    您可以在此处查看兼容性表@MDN


    一个建议是使用回退来从服务器获取文件大小。

    喜欢:

    if(IE > 9){
       // use FileApi
    }else{
       // put a call to the server to get the 
       // file size. an ajax call would be fine.
    }
    

    【讨论】:

    • 如果我进行 ajax 调用并且文件超过 2 gb,你不认为它会引发错误。因为在 asp.net 中我们可以上传 fie 到 2 gb 我想禁止用户上传 2GB
    猜你喜欢
    • 2010-11-18
    • 1970-01-01
    • 2014-11-29
    • 2022-01-10
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多