限制具有特定大小的文件上载

时间:2015-08-23 07:29:27

标签: javascript html asp.net vb.net

我有vb.net网络应用程序,我在互联网上搜索代码以限制用户上传超过10MB的文件,但是,在我调试应用程序后使用的代码工作之后我的视觉工作室。

现在,当我将调试的文件移动到服务器时,它不起作用!让我感到更困扰的是代码在谷歌浏览器中工作得很好但在互联网资源管理器中没有工作!!!

Internet Explorer版本11.0.22,Google Chrome 44.0版



<%@ Page Language="VB" %>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta charset="utf-8" />
    <title></title>    
</head>
<body>
    <form id="Form1" method="post" runat="server">
			<table>
                <tr>
                    <td width="10%" align="left">
                        <asp:Label Style="z-index: 0" ID="Label10" runat="server" Font-Names="Comic Sans MS" Font-Size="X-Small"> ATTACHMENT (IF AVALIABLE)</asp:Label></td>
                    <td width="90%">
                        <input style="z-index: 0" id="MyFile" type="file" name="myFile" runat="server">
                        <asp:Label Style="z-index: 0" ID="Label17" runat="server" Font-Bold="True" Width="197px" ForeColor="Red">Maximum File Size = 10MB</asp:Label></td>
                    <td width="1%"></td>
                </tr>
                <tr>
                    <td width="10%" align="left"></td>
                    <td width="90%">
                        <asp:Button Style="z-index: 0" ID="Button1" runat="server" Font-Names="Comic Sans MS" Height="32px"
                            Width="112px" Text="SUBMIT" BorderColor="#404040" BorderStyle="Solid" BorderWidth="1px"></asp:Button></td>
                    <td width="1%"></td>
                </tr>
            </table>
    </form>
    <input type="hidden" value="0" name="fisize" id="fisize" />
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <script type="text/javascript">
            var confirm_value = document.createElement("INPUT");
            confirm_value.type = "hidden";
            confirm_value.name = "confirm_value";                
            $(document).ready(function () {
            $('#<%= Page.FindControl("MyFile").ClientID%>').change(function() {
            //$("#MyFile").change(function () {
           //var fi = document.getElementById("MyFile");
            var fi =document.getElementById('<%=Page.FindControl("MyFile").ClientID%>');
                    var dd = fi.files[0].size;
                    if (dd > 10485760) {
                        alert("the selected file size is more than 10MB, please select another file !!");
                        confirm_value.value = "No";
                    }
                    else {
                        alert("okay :)");
                        confirm_value.value = "Yes";
                    }
                    document.forms[0].appendChild(confirm_value);
                });
            });
        </script>
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

为什么不使用ASP.NET文件上传控件?

然后:

int max = 2097152; // ~2MB = 1024 * 2048
if (FileUpload1.HasFile && FileUpload1.PostedFile.ContentLeng­th <= max)
{
   //save

}