我正在处理用户可以通过asp:fileupload提交文件的表单,但是如果用户上传的文件超过10MB,则希望显示特定的错误消息。但是,当我输入try catch语句时,我的错误消息不会显示,只显示默认消息。如何确保它显示?
Try
If fuAttach.HasFile Then
Dim filename As String = fuAttach.FileName
fuAttach.SaveAs(Server.MapPath("~/") + filename)
Dim reqContext As SoapContext = SDWS.RequestSoapContext
Dim dimeAttach As New DimeAttachment("image/gif", TypeFormat.MediaType, HttpContext.Current.Request.MapPath("~/" & fuAttach.FileName))
reqContext.Attachments.Add(dimeAttach)
SDWS.createAttachment(SID, "doc_rep:400842", objectHandle, "description", fuAttach.FileName)
File.Delete(Server.MapPath("~/") + filename)
lblThankyou.Text = "<p style = ""font-size: small"">Thank you, your request has been submitted!</br></font></br></br>You will receive an email in several minutes from autonotify@arlingtonva.us confirming that a ticket has been entered into our system.</p>"
Else
lblThankyou.Text = "<p style = ""font-size: small"">Thank you, your request has been submitted!</br></font></br></br>You will receive an email in several minutes from autonotify@arlingtonva.us confirming that a ticket has been entered into our system.</p>"
End If
Catch
lblThankyou.Text = "<p style = ""font-size: small"">Your file upload may not exceed 10MB</br></font></br></br></p>"
End Try
答案 0 :(得分:1)
你应该可以在这里找到一些答案:
https://stackoverflow.com/search?q=asp.net+max+file+upload+size&submit=search
<强>更新强>
为最大15MB的文件设置:
<configuration>
<system.web>
<httpRuntime maxRequestLength="15360" />
</system.web>
</configuration>
答案 1 :(得分:0)
在上传之前,您将无法检查文件大小,因为服务器尚未收到整个文件。 并且在浏览器中没有用于文件系统访问的本机API。我发现有些人使用ActiveX文件系统对象在IE here中使用javascript完成此操作,但它不适用于其他浏览器。
在ASP.NET中处理大型文件上传有更好的解决方案。自定义HttpHandler可以通过显示上载进度并允许您以更加可控的方式处理文件大小问题来提供更好的用户体验。如果你感兴趣,有一长串论坛帖子here。
您还可以考虑第三方控件,例如免费的NeatUpload。