在ASP.NET中實現多檔案上傳 (轉)

amyz發表於2007-08-15
在ASP.NET中實現多檔案上傳 (轉)[@more@]

在中實現多

在以前的應用中,上傳檔案是個很麻煩的事,現在有了.NET,檔案上傳變得輕而易舉。下面的這個例子實現了多檔案上傳功能。可以動態新增輸入表單,上傳的檔案數量沒有限制。程式碼如下:

playground.com/Multi." target=_blank>檢視例子

MultiUpload.aspx

w3c//DTD HTML 4.0 Transitional//EN&quot; &gt; <title>多檔案上傳</title> <fo href="http://blog.itpub.net/10752019/viewspace-957931/tag-415-1.html">RM id=&quot;form1&quot; method=&quot;post&quot; runat=&quot;server&quot; enctype=&quot;multipart/form-data&quot;&gt; <center> gerStepThrough()&gt; Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System., ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load MyTitle.Text = &quot;<h3>多檔案上傳</h3>&quot; Upload.Text = &quot;開始上傳&quot; If (Me.IsPostBack) Then Me.SaveImages() End Sub Private Function SaveImages() As System.Boolean '遍歷File表單元素 Dim files As System.Web.HttpFileCollection = System.Web.HttpContext.Current.Request.Files '狀態資訊 Dim strMsg As New System.Text.StringBuilder(&quot;上傳的檔案分別是:<hr color="red">&quot;) Dim iFile As System.Int32 Try For iFile = 0 To files.Count - 1 '檢查副檔名字 Dim postedFile As System.Web.HttpPostedFile = files(iFile) Dim fileName, fileExtension As System.String fileName = System.IO.Path.GetFileName(postedFile.FileName) If Not (fileName = String.Empty) Then fileExtension = System.IO.Path.GetExtension(fileName) strMsg.Append(&quot;上傳的檔案型別:&quot; + postedFile.ContentType.ToString() + &quot;<br>&quot;) strMsg.Append(&quot;客戶端檔案地址:&quot; + postedFile.FileName + &quot;<br>&quot;) strMsg.Append(&quot;上傳檔案的檔名:&quot; + fileName + &quot;<br>&quot;) strMsg.Append(&quot;上傳檔案的副檔名:&quot; + fileExtension + &quot;<br><hr>&quot;) '可根據副檔名字的不同儲存到不同的資料夾 postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath(&quot;images/&quot;) + fileName) End If Next strStatus.Text = strMsg.ToString() Return True Catch Ex As System.Exception strStatus.Text = Ex.Message Return False End Try End Function End Class </center></fo>

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-957931/,如需轉載,請註明出處,否則將追究法律責任。

相關文章