2

I have been working on a file upload section within my ASP.NET site.

With the below code, I am able to get a user to upload documents based based on the Regular Expressions set within the RegularExpressionValidator. I am happy that this works accordingly.

What I would like to complete now is a message to indicate that the file has been uploaded successfully. I am unsure how to complete this, but would like to add it to a Label named "fileuploaded".

Here is my code for the .aspx page:

<table width = "60%">
  <tr>
    <td>Modes of Operation:</td>
    <td>
       <asp:FileUpload ID="FileUpload1" runat="server" />
    </td>
    <td>
       <asp:Button ID="buttonUpload" runat="server" Text="Upload" ValidationGroup="FileUpload" />
    </td>
  </tr>
  <tr>
    <td colspan="3">
       <asp:RequiredFieldValidator ID="FilenameRFValidator" runat="server" 
            ControlToValidate="FileUpload1" Display="Dynamic" 
            ErrorMessage="RequiredFieldValidator" ValidationGroup="FileUpload"> 
            * Please select a file to upload...
       </asp:RequiredFieldValidator></td>
   </tr>
   <tr>
     <td colspan="3">
        <asp:RegularExpressionValidator ID="FilenameRegExValidator" runat="server" 
             ControlToValidate="FileUpload1" Display="Dynamic" 
             ErrorMessage="RegularExpressionValidator" 
             ValidationExpression="(?i)^[\w\s0-9.-]+\.(txt|pdf|doc|docx|xls|xlsx)$" 
             ValidationGroup="FileUpload">
             * Please upload file in format .pdf / .docx / .xlsx.
        </asp:RegularExpressionValidator>
     </td>
   </tr>
  </table>   
        <asp:Label ID="lblfileuploaded" runat="server" Text=""></asp:Label>

And here is my code so far for the VB page:

Protected Function GetUploadList() As String()
            Dim folder As String = Server.MapPath("~/Uploads")
    Dim files() As String = Directory.GetFiles(folder)
    Dim fileNames(files.Length - 1) As String
    Array.Sort(files)

    For i As Integer = 0 To files.Length - 1
        fileNames(i) = "<a href=""Uploads/" & Path.GetFileName(files(i).ToString()) & """ target=""_blank"">" & Path.GetFileName(files(i)) & "</a>"
    Next

    Return fileNames
End Function

   Protected Sub UploadThisFile(ByVal upload As FileUpload)
    If upload.HasFile Then
        Dim theFileName As String = Path.Combine(Server.MapPath("~/Uploads"), upload.FileName)

        If File.Exists(theFileName) Then
            File.Delete(theFileName)
        End If

        upload.SaveAs(theFileName)
    End If
End Sub

Protected Sub buttonUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonUpload.Click
    UploadThisFile(FileUpload1)
    UploadedFiles.DataBind()
End Sub

Any help in advance is much appreciated.

4

1 に答える 1

1

ティム・シュメルターがコメントで述べているように、SaveAs正常に呼び出された後、ラベルのテキストを設定する必要があります。

Try->を使用して、Catch例外がないことを確認できます(上記のリンク先のMSDNの記事によると、SaveAsメソッドはをスローする可能性がありますHttpException)。このようなもの:

Try
    upload.SaveAs(theFileName)
    fileuploaded.Text="File uploaded successfully"
Catch ex As Exception
    fileuploaded.Text="Upload failed.  Reason: " + ex.Message
于 2012-05-16T13:49:36.610 に答える