我一直在研究ASP.NET網站中的文件上傳部分。在文件上傳時顯示的簡單消息
使用下面的代碼,我可以讓用戶上傳基於RegularExpressionValidator
中設置的正則表達式的文檔。我很高興這相應地起作用。
我現在要完成的是一條消息,指出文件已成功上傳。我不確定如何完成此操作,但希望將其添加到名爲「fileuploaded」的Label
。
這裏是我在.aspx頁面代碼:
<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>
這裏是我到目前爲止的代碼爲VB頁:
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
提前任何幫助深表感謝。
'fileuploaded.Text =「上傳成功」'upload.aveAs(theFileName)'''後成功了嗎? –
@tim。這是一種享受。非常感謝您的幫助。 – Betty