Asp.net 如何在上传大于限制大小的文件后显示消息框,而不是显示IE页面错误

Asp.net 如何在上传大于限制大小的文件后显示消息框,而不是显示IE页面错误,asp.net,vb.net,internet-explorer,file-upload,filesize,Asp.net,Vb.net,Internet Explorer,File Upload,Filesize,在我的项目中,我有FileUpload控件和upload按钮。我想将文件大小限制为10MB,因此我将以下代码放在web.config中 <system.web> <httpRuntime executionTimeout="240" maxRequestLength="10240" /> </system.web> 我还将try/catch块放在upload button click方法中,如下所示: Private Sub Upload_Cli

在我的项目中,我有FileUpload控件和upload按钮。我想将文件大小限制为10MB,因此我将以下代码放在web.config中

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

我还将try/catch块放在upload button click方法中,如下所示:

Private Sub Upload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_Attach.Click
    Try
        If FileUpload1.HasFile Then
            Dim fileSize As Integer = FileUpload1.PostedFile.ContentLength
            If (fileSize < 10485760) Then
                Select_id = ViewState("Select_id")
                typename = CInt(ViewState("typename"))
                If Select_id.HasValue Then
                    UploadFile(FileUpload1.PostedFile)
                End If
            Else
                ShowMessageBox(Me.Parent.Page, "File size cannot be greater than 10MB.")
            End If
        End If
    Catch ex As Exception
        ShowMessageBox(Me.Parent.Page, ex.Message)
    End Try
End Sub
Private Sub Upload\u Click(ByVal sender作为对象,ByVal e作为System.EventArgs)处理btn\u Attach.Click
尝试
如果FileUpload1.HasFile,则
Dim fileSize As Integer=FileUpload1.PostedFile.ContentLength
如果(文件大小<10485760),则
选择_id=ViewState(“选择_id”)
typename=CInt(视图状态(“typename”))
如果选择_id.HasValue,则
上传文件(FileUpload1.PostedFile)
如果结束
其他的
ShowMessageBox(Me.Parent.Page,“文件大小不能大于10MB”)
如果结束
如果结束
特例
ShowMessageBox(Me.Parent.Page,例如Message)
结束尝试
端接头
当我测试该函数时,它对大小小于10MB的文件正常工作。相反,大于10MB的文件会导致页面错误,说明“Internet Explorer无法显示网页”。当我尝试调试时,它甚至没有进入按钮单击方法


现在,这并不是什么大问题。我只是想知道如何显示错误消息框,而不是重定向到IE错误页面。如果没有解决方案,我就不做了。

您使用的是web还是windows应用程序?你的按钮id是什么?很抱歉回复晚了,我正在使用web应用程序。我的按钮id是“btn_Attach”。无论如何,我刚从我的队友那里得到一个想法。他建议我将maxRequestLength增加到10MB以上,然后在上传之前在代码隐藏中检查文件大小。它工作得很好。