我已经检查了文件大小和文件扩展名。但是我还需要检查上传过程是否超时了。如果是这样,我想向用户显示一条消息,告诉他们上传超时了,他们可以稍后再试。任何帮助将非常感激!:)
Dim success As Boolean = False
Response.Write("*EIL*")
Try
If Not Context.Request.Files Is Nothing Then
Dim fileCount As Int32 = Context.Request.Files.Count
For fileLoop As Integer = 0 To fileCount - 1
Dim file As HttpPostedFile = Context.Request.Files(fileLoop)
Dim fileName As String
If file.ContentLength > 20971520 Then
Response.Write("The upload failed because the file size is too large - 20MB is the limit.")
Else
fileName = HttpUtility.UrlDecode(file.FileName)
Dim ext As String = fileName.Substring(fileName.Length - 4, 4).ToLower
If ext = ".jpg" Or ext = ".gif" Or ext = ".png" Or ext = ".bmp" Or ext = ".psd" Or ext = ".tif" Then
If InStr(fileName, "") > 0 Then
Dim arr() As String = Split(fileName, "")
fileName = arr(arr.Length - 1)
End If
file.SaveAs(String.Format(ConfigurationManager.AppSettings("ArtworkUploadPath"), fileName))
success = True
Else
Response.Write("The upload failed because the file was the wrong type. Only files with the following extensions are allowed: .jpg, .gif, .png, .bmp, .psd, .tif")
End If
End If
Next
If success Then Response.Write("Success")
End If
Catch ex As Exception
End Try
短答:你不能。
较长的答案:当上传超时时,这是因为服务器认为您的代码花费的时间太长,杀死您的进程,然后抛出异常。因此,当超时发生时,您的代码不再运行,因此无法向用户显示消息。
你可以记录超时,如果你想,在一个类似的问题中解释。
理想情况下,你不想捕捉超时,你想修复你的页面,使其不会超时。您可以通过以下几种方式实现:
-
更改
system.web
中maxRequestLength
的值以限制允许的文件大小,并在用户上传超过该值时向用户显示错误页面 -
通过增加
system.web
中executionTimeout
的值来延长超时时间,如这里的答案所示。