开发者

I need help to find a way to calculate the file size of uploaded file - HttpContext.Current.Request.Files

开发者 https://www.devze.com 2022-12-16 21:54 出处:网络
I need to calculate the file size of uploaded file in order to catch System.Web.HttpException: Maximum request length exceeded.

I need to calculate the file size of uploaded file in order to catch System.Web.HttpException: Maximum request length exceeded. This is my code

    <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>

The Click Event handler:

    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);
            }
        }
    }

I changed the file size in web.config.

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


The following will get you the file sizes, but the problem you'll have is that the "Maximum request length exceeded" error will be thrown before you get a chance to do anything with it:

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;
}

So what you'll actually have to do is catch the exception in either an Application error handler, or on a custom 500 internal server error page, and display a meaningful message to the user.

JavaScript won't really help you either, as security concerns mean that you can no longer interact with the selected files on the client side.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号