【问题标题】:Visual Basic Max File Size ErrorVisual Basic 最大文件大小错误
【发布时间】:2015-05-22 15:45:41
【问题描述】:
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)我会收到错误

块引用 “/”应用程序中的服务器错误

已超出最大请求长度。

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

我怎样才能越过错误屏幕并告诉用户上传一个较小的文件?

【问题讨论】:

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


    【解决方案1】:

    您对If FileUploader.HasFile Thenelse 声明没有意义。如果没有要上传的文件,您如何检查文件大小?将子嵌套的 if 语句 (If fileSize &gt; 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多