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

來源:互聯網
上載者:User

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

MultiUpload.aspx

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="MultiUpload.aspx.vb"Inherits="aspxWeb.MultiUpload" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><HTML><HEAD><title>多檔案上傳</title><script language="JavaScript">function addFile(){var str = '<INPUT type="file" size="50" NAME="File">'document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)}</script></HEAD><body><form id="form1" method="post" runat="server" enctype="multipart/form-data"><center><asp:Label Runat="server" ID="MyTitle"></asp:Label><P id="MyFile"><INPUT type="file" size="50" NAME="File"></P><P><input type="button" value="增加(Add)" onclick="addFile()"><asp:Button Runat="server" Text="上傳" ID="Upload"></asp:Button><input onclick="this.form.reset()" type="button" value="重設(ReSet)"></P></center><P align="center"><asp:Label id="strStatus" runat="server" Font-Names="宋體" Font-Bold="True"Font-Size="9pt" Width="500px" BorderStyle="None" BorderColor="White"></asp:Label></P></form></body></HTML>

後代碼:MultiUpload.aspx.vb

Public Class MultiUploadInherits System.Web.UI.PageProtected WithEvents Upload As System.Web.UI.WebControls.ButtonProtected WithEvents MyTitle As System.Web.UI.WebControls.LabelProtected WithEvents strStatus As System.Web.UI.WebControls.Label#Region " Web Form Designer Generated Code "'This call is required by the Web Form Designer.<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()End SubPrivate Sub Page_Init(ByVal sender As System.Object, 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 RegionPrivate Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadMyTitle.Text = "<h3>多檔案上傳</h3>"Upload.Text = "開始上傳"If (Me.IsPostBack) Then Me.SaveImages()End SubPrivate 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("上傳的檔案分別是:<hr color=red>")Dim iFile As System.Int32TryFor iFile = 0 To files.Count - 1'檢查副檔名字Dim postedFile As System.Web.HttpPostedFile = files(iFile)Dim fileName, fileExtension As System.StringfileName = System.IO.Path.GetFileName(postedFile.FileName)If Not (fileName = String.Empty) ThenfileExtension = System.IO.Path.GetExtension(fileName)strMsg.Append("上傳的檔案類型:" + postedFile.ContentType.ToString() + "<br>")strMsg.Append("用戶端檔案地址:" + postedFile.FileName + "<br>")strMsg.Append("上傳檔案的檔案名稱:" + fileName + "<br>")strMsg.Append("上傳檔案的副檔名:" + fileExtension + "<br><hr>")'可根據副檔名字的不同儲存到不同的檔案夾postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/") + fileName)End IfNextstrStatus.Text = strMsg.ToString()Return TrueCatch Ex As System.ExceptionstrStatus.Text = Ex.MessageReturn FalseEnd TryEnd FunctionEnd Class

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.