我需要计算上传文件的文件大小以捕获System.Web.HttpException:超出最大请求长度。 这是我的代码
<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>
Click事件处理程序:
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" />
答案 0 :(得分: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也不会真正帮助您,因为安全问题意味着您无法再与客户端上的选定文件进行交互。