【问题标题】:How to display error when no file selected未选择文件时如何显示错误
【发布时间】:2012-09-05 17:31:06
【问题描述】:

我们的经典 ASP 应用程序使用一个 .aspx 页面为每个客户端上传图像。 我不熟悉 ASP.net,所以这个页面对我来说很难理解。

<html>
<head>
    <link href="css/style.css" rel="stylesheet" type="text/css" />
    <script language="VB" RunAt="Server">

        Sub Page_Load(Sender As Object, e As EventArgs)
            Span1.InnerHtml = "You may not upload a file larger than 250 Kilobytes. Your image must be in JPG format.<br />Please crop your image to 400 pixels high and 300 pixels wide, so that no distortion will occur when displayed.<br /><br />"
        End Sub

        Sub Upload_Click(Sender As Object, e As EventArgs)
            Dim DistributorID As Integer
            DistributorID = request.QueryString("distID")

            ' Display properties of the uploaded file

            'Let us recover only the file name from its fully qualified path at client 

            Dim strFileName As String
            strFileName = MyFile.PostedFile.FileName
            Dim c As String = CStr(DistributorID) & "." & split(System.IO.Path.GetFileName(strFileName), ".")(1) ' The file name is the distid + the extension

            'Let us Save uploaded file to server 
            If LCase(Split(System.IO.Path.GetFileName(strFileName), ".")(0)) = "" Then
                Span1.InnerHtml = "Please select a Photo.<br /><br />"
            End If

            If LCase(Split(System.IO.Path.GetFileName(strFileName), ".")(1)) <> "jpg" Then
                Span1.InnerHtml = "Invalid file format."
            Else

                If MyFile.PostedFile.ContentLength < 250000 Then
                    MyFile.PostedFile.SaveAs(ConfigurationSettings.AppSettings("DistImagePath") + c)
                    Span1.InnerHtml = "Your file imported sucessfully to the server.<br /><br />"
                Else
                    Span1.InnerHtml = "Your file is too large to import.<br /><br />"
                End If
            End If

        End Sub

    </script>
</head>
<body>      
    <span id="Span1" style="color:Red;" RunAt="Server" />                   
    <form method="Post" enctype="Multipart/Form-Data" RunAt="Server" class="stdform stdform2">
        &nbsp;&nbsp;Select New Photo: <br />
        &nbsp;<input id="MyFile" type="File" runat="Server" size="40" /><br /><br />            
        <span style="padding-left:25px;">
            <button type="Submit" value="Import" OnServerclick="Upload_Click" RunAt="Server">Upload New Photo</button>
        </span>
    </form>
</body>

如果没有选择文件并且按下上传新照片按钮,我会得到一个 “指数数组的边界之外。”错误

如果他们没有选择照片,我如何修复此页面以不尝试上传照片?

【问题讨论】:

    标签: asp.net image error-handling


    【解决方案1】:

    您可以将所有内容都包含在 IF 中:

    IF MyFile.PostedFile.ContentLength>0 Then 'User selected a file
    
       'All your code here
    Else
        Span1.InnerHtml = "Your did not select a file.<br /><br />"
    End IF
    

    完整代码:

     Sub Upload_Click(Sender As Object, e As EventArgs)
        IF MyFile.PostedFile.ContentLength>0  Then   
            Dim DistributorID As Integer
            DistributorID = request.QueryString("distID")
    
            ' Display properties of the uploaded file
    
            'Let us recover only the file name from its fully qualified path at client 
    
            Dim strFileName As String
            strFileName = MyFile.PostedFile.FileName
            Dim c As String = CStr(DistributorID) & "." & split(System.IO.Path.GetFileName(strFileName), ".")(1) ' The file name is the distid + the extension
    
            'Let us Save uploaded file to server 
            If LCase(Split(System.IO.Path.GetFileName(strFileName), ".")(0)) = "" Then
                Span1.InnerHtml = "Please select a Photo.<br /><br />"
            End If
    
            If LCase(Split(System.IO.Path.GetFileName(strFileName), ".")(1)) <> "jpg" Then
                Span1.InnerHtml = "Invalid file format."
            Else
    
                If MyFile.PostedFile.ContentLength < 250000 Then
                    MyFile.PostedFile.SaveAs(ConfigurationSettings.AppSettings("DistImagePath") + c)
                    Span1.InnerHtml = "Your file imported sucessfully to the server.<br /><br />"
                Else
                    Span1.InnerHtml = "Your file is too large to import.<br /><br />"
                End If
            End If
         Else
                Span1.InnerHtml = "Your did not select a file.<br /><br />"
         End IF
    
        End Sub
    

    【讨论】:

    • 那么您是否建议将 IF 语句放在 Upload_Click Sub 周围?
    【解决方案2】:
    If MyFile.PostedFile.ContentLength>0 
      ..
    Else
       Span1.InnerHtml = "You have not specified a file."
    End If
    

    您可以在执行之前添加此代码(如果您要执行上传文件)

    If FileUpload1.HasFile Then
    
        ......
    
    Else
          Span1.InnerHtml = "You have not specified a file."
    End If
    

    【讨论】:

    • 不,他没有使用常规的 ASP.NET 文件上传控件...查看标记
    猜你喜欢
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    相关资源
    最近更新 更多