【问题标题】:I need help to find a way to calculate the file size of uploaded file - HttpContext.Current.Request.Files我需要帮助找到一种方法来计算上传文件的文件大小 - HttpContext.Current.Request.Files
【发布时间】:2010-01-14 14:08:45
【问题描述】:

我需要计算上传文件的文件大小才能捕获 System.Web.HttpException: Maximum request length exceeded。 这是我的代码

    <table border="0" cellpadding="2" cellspacing="2" width="90%" style="margin-top:20px; margin-bottom:40px; padding:30px;">
        <tr>
            <td align="right" style="width:140px">
                <asp:Label ID="lblTicketName" runat="server" Text="Ticket Name:" Font-Bold="true" />
            </td>
            <td colspan="2">
                <asp:TextBox ID="tbTicketName" runat="server" Width="315" />
            </td>
        </tr>
        <tr>
            <td align="center" colspan="3">
                <asp:Label ID="lblTicketDescription" runat="server" Text="Ticket description" Font-Bold="true" />
            </td>
        </tr>
        <tr>
            <td align="center" colspan="3">
                <asp:TextBox ID="tbTicketDescription" runat="server" Height="100" Width="450" />
            </td>
        </tr>
        <tr>
            <td align="right">
                <asp:Label ID="lblAttachFiles" runat="server" Text="Attach files:" Font-Bold="true" />
            </td>
            <td>
                <p id="upload-area">
                    <asp:FileUpload ID="fuAttachFiles" runat="server" />
                </p>
            </td>
            <td>
                <input id="btnAddMoreFiles" type="button" value="Add another file" onclick="addFileUploadBox()" />
            </td>
        </tr>
        <tr>
            <td align="right">
                <asp:Label ID="lblType" runat="server" Text="Ticket type:" Font-Bold="true" />
            </td>
            <td>
                <asp:DropDownList ID="ddlType" runat="server" >
                    <asp:ListItem Text="Bug report" Value="BugReport" />
                    <asp:ListItem Text="Feature request" Value="FeatureRequest" />
                    <asp:ListItem Text="Assistance request" Value="AssistanceRequest" />
                    <asp:ListItem Text="Other" Value="Other" />
                </asp:DropDownList>
            </td>
            <td>
                <asp:Button ID="btnCreateNewTicket" runat="server" Text="Create new ticket" 
                    onclick="btnCreateNewTicket_Click" />
            </td>
        </tr>
    </table> 
    <script type="text/javascript">
        function addFileUploadBox() {
            if (!document.getElementById || !document.createElement)
                return false;

            var uploadArea = document.getElementById("upload-area");

            if (!uploadArea)
                return;

            var newLine = document.createElement("br");
            uploadArea.appendChild(newLine);

            var newUploadBox = document.createElement("input");

            // Set up the new input for file uploads
            newUploadBox.type = "file";

            // The new box needs a name and an ID
            if (!addFileUploadBox.lastAssignedId)
                addFileUploadBox.lastAssignedId = 100;

            newUploadBox.setAttribute("id", "dynamic" + addFileUploadBox.lastAssignedId);
            newUploadBox.setAttribute("name", "dynamic:" + addFileUploadBox.lastAssignedId);
            uploadArea.appendChild(newUploadBox);
            addFileUploadBox.lastAssignedId++;
        }
    </script>

点击事件处理程序:

    protected void btnCreateNewTicket_Click(object sender, EventArgs e)
    {
        var uploads = HttpContext.Current.Request.Files;

        var filesExist = false;
        if (uploads[1].ContentLength != 0)
            filesExist = true;

        var ticketId = Ticket.Tickets.CreateTicket(int.Parse(ProjectId), TsSession.Current.Username, tbTicketName.Text, ddlType.SelectedValue, tbTicketDescription.Text, filesExist);

        if (filesExist)
        {
            var uploadPath = "G:\\VS\\Ticketing System2\\UploadedFiles\\" + ProjectId + "\\" + ticketId + "\\TicketFiles\\";

            if (!Directory.Exists(uploadPath))
                Directory.CreateDirectory(uploadPath);

            for (var i = 0; i < uploads.Count; i++)
            {
                var fileName = Path.GetFileName(uploads[i].FileName);

                uploads[i].SaveAs(uploadPath + fileName);
            }
        }
    }

我在 web.config 中更改了文件大小。

<system.web>
<httpRuntime maxRequestLength="10240" />

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    以下将为您提供文件大小,但您将遇到的问题是在您有机会对其进行任何操作之前会抛出“超出最大请求长度”错误:

    HttpFileCollection Files;
    int fileLength;
    
    Files = Request.Files; // Load File collection into HttpFileCollection variable.
    arr1 = Files.AllKeys;  // This will get names of all files into a string array.
    for (int i = 0; i < arr1.Length; i++) 
    {
        fileLength += Files[i].ContentLength;
    }
    

    因此,您实际上需要做的是在应用程序错误处理程序或自定义 500 内部服务器错误页面上捕获异常,并向用户显示有意义的消息。

    JavaScript 也不会真正帮助您,因为安全问题意味着您无法再与客户端上的选定文件进行交互。

    【讨论】:

    • 您使用 arr 只是为了计数?为什么不使用 Request.Files.Count?
    • 我将手动检查文件长度和最大长度并显示一条消息。
    • 我试过:for(var i = 0; i 100000000) { Ticketing_System2.Error.HandleError("文件" + Path.GetFileName(uploads[i].FileName) + " 大于 10mb。");返回; } } 但我仍然得到异常。
    • @Chris 好点 - 从文档 (msdn.microsoft.com/en-us/library/…) 中草率地复制和粘贴。 @Ivanst - 是的,这就是我所说的会发生 - 你有机会查看集合之前引发异常 - 你需要编写一个应用程序级错误处理程序来捕获异常,并向用户显示有意义的消息。
    猜你喜欢
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多