Visual Basic 最大文件大小错误

Visual Basic Max File Size Error

Protected Sub AddFileButton_Click(ByVal sender As Object, _
     ByVal e As System.EventArgs)  
        Dim fileSize = FileUploader.PostedFile.ContentLength
    If FileUploader.HasFile Then    
        
        Try
            Dim extension = System.IO.Path.GetExtension(FileUploader.FileName)
            Dim uniqueFileName = System.Guid.NewGuid.ToString() & extension
            FileUploader.SaveAs("\path\" & FileUploader.FileName)
        Catch ex As Exception
            Info.Text = "ERROR: " & ex.Message.ToString()
        End Try
        
    Else
         If fileSize > 1048576 Then
            Info.Text = "This file exceeds the allowed file size (1 MB). Please resize the image or select another file."
             return
         
         ElseIf fileSize < 1 Then
            Info.Text = "This file does not have enough content to send. Please choose another file."
            return
         End If   
    End If
End Sub

大家好!我有一个快速的问题。我正在尝试处理最大文件大小错误。

如果文件太小,它会起作用。但是,如果文件太大(1mb),我会收到错误消息

Blockquote Server Error in '/' Application

Maximum request length exceeded.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

我如何跳过错误屏幕并告诉用户上传较小的文件?

您对 If FileUploader.HasFile Thenelse 陈述没有意义。如果没有要上传的文件,您如何检查文件大小?将子嵌套 if 语句 (If fileSize > 1048576 Then) 移动到主 IF 语句中。像这样:

If FileUploader.HasFile Then    

     If fileSize > 1048576 Then
        Info.Text = "This file exceeds the allowed file size (1 MB). Please resize the image or select another file."
         return

     ElseIf fileSize < 1 Then
        Info.Text = "This file does not have enough content to send. Please choose another file."
        return
     Else
         Try
             Dim extension = System.IO.Path.GetExtension(FileUploader.FileName)
             Dim uniqueFileName = System.Guid.NewGuid.ToString() & extension
             FileUploader.SaveAs("\filepath\" & FileUploader.FileName)
         Catch ex As Exception
             Info.Text = "ERROR: " & ex.Message.ToString()
         End Try
     End If   

End If