2008年4月22日 星期二

FileUpload控制項,檔案上傳

以前寫ASP要靠第三協力廠商提供的控制項才能上傳,現在有了「FileUpload」控制項,變得很簡單。

底下這兩個網址,MS提供的範例非常精彩且實用!照抄都能學到東西~棒!
http://msdn2.microsoft.com/zh-tw/library/system.web.ui.webcontrols.fileupload.aspx

http://msdn2.microsoft.com/zh-tw/library/ms227669.aspx



Protected Sub Page_Load(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack Then
Dim path As String = Server.MapPath("~/UploadedImages/")
Dim fileOK As Boolean = False
If FileUpload1.HasFile Then
Dim fileExtension As String
fileExtension = System.IO.Path. _
GetExtension(FileUpload1.FileName).ToLower()
Dim allowedExtensions As String() = _
{".jpg", ".jpeg", ".png", ".gif"}
For i As Integer = 0 To allowedExtensions.Length - 1
If fileExtension = allowedExtensions(i) Then
fileOK = True
End If
Next
If fileOK Then
Try
FileUpload1.PostedFile.SaveAs(path & _
FileUpload1.FileName)
Label1.Text = "File uploaded!"
Catch ex As Exception
Label1.Text = "File could not be uploaded."
End Try
Else
Label1.Text = "Cannot accept files of this type."
End If
End If
End If
End Sub

沒有留言: