Visual Basic最大文件大小错误

时间:2015-05-22 15:45:41

标签: asp.net .net vb.net file-upload error-handling

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),我收到错误

  

块引用   &#39; /&#39;中的服务器错误应用

     

超出最大请求长度。

     

说明:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中的起源位置的更多信息。

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

1 个答案:

答案 0 :(得分:1)

else的{​​{1}}声明没有意义。如果没有要上传的文件,您如何检查文件大小?将subnested if语句(If FileUploader.HasFile Then)移动到主IF语句中。像这样:

If fileSize > 1048576 Then