1

我已经检查了文件大小和文件扩展名。但是我仍然需要检查上传过程是否超时。如果确实如此,我想向用户显示上传超时的消息,他们可以稍后再试。任何帮助将不胜感激!:)

    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
4

1 回答 1

0

简短的回答:你不能。

更长的答案:当上传超时时,这是因为服务器决定您的代码花费太长时间,杀死您的进程,然后抛出异常。因此,当超时发生时您的代码不再运行,因此无法向用户显示消息。

您可以根据需要记录超时,如类似问题中所述。

理想情况下,您无论如何都不想赶上超时,您想修复您的页面,使其不会超时。您可以通过以下几种方式做到这一点:

  • 更改maxRequestLength您的值system.web以限制允许的文件大小,并在用户上传超过该值时向用户显示错误页面。

  • 通过增加executionTimeoutin your的值来延长超时期限system.web,如在此处找到的答案所示

于 2013-01-09T22:41:52.827 回答