在没有ActiveX对象的情况下在javascript中获取文件的大小 [英] Getting size of file in javascript without ActiveX Object

查看:77
本文介绍了在没有ActiveX对象的情况下在javascript中获取文件的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用asp.net(C#)中的文件上传控件来验证上传文件的大小,该大小不超过200kb.
我试过了

I want to validate size of a uploaded file using file upload control in asp.net(C#), which is not more than 200kb.
I tried

<script language="javascript" type="text/javascript">
    function getFileSize()
    {
    debugger;
   // alert("hello");
    var strFileName= document.getElementById("FUAttachContract").value;
    //alert(strFileName);
    var objFSO = new ActiveXObject("Scripting.FileSystemObject");
   // alert(objFSO);
    var e = objFSO.getFile(strFileName);
    var fileSize = e.size;
    alert(fileSize);
        if (fileSize > 204800) {
            alert('size more than 200KB');
        }
    }
    </script>


<form id="form1" runat="server">
       <asp:FileUpload ID="FUAttachContract" runat="server" />
       <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="javascript:getFileSize();" />
   </form>



当我手动启用ActiveX对象时,它可以正常工作,我希望在不使用ActiveX对象的情况下提出建议.



Its working fine when I manually enable the ActiveX object, I want suggestion without using ActiveX Object.

推荐答案

是的,W3C有一个新功能,该功能是一些现代浏览器支持的File API.它可以用于此目的,并且很容易测试它是否受支持,如果不支持,则可以回退到其他机制(如果需要).

http://stackoverflow.com/a/3717847/540653 [
Yes, there''s a new feature from the W3C that''s supported by some modern browsers, the File API. It can be used for this purpose, and it''s easy to test whether it''s supported and fall back (if necessary) to another mechanism if it isn''t.

http://stackoverflow.com/a/3717847/540653[^]


我的建议,如果您要避免使用ActiveX路由,并且想要要确保没有上传超过一定长度的文件,则可以在配置文件中设置大小限制:
My suggestion, if you want to avoid ActiveX route, and you want to make sure that file above a certain length is not uploaded then you can set the size limit in config file:
<system.web>
    <httpRuntime  maxRequestLength="2048"/>
</system.web>


它的缺点:任何大于定义大小的文件都将导致页面显示此页面无法显示",因此您需要捕获此错误并进行处理.


Down side of it: any file greater than defined size would lead page to display "This page cannot be displayed" so you need to catch this error and handle it.


这篇关于在没有ActiveX对象的情况下在javascript中获取文件的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆