在ASP.NET中实现多文件上传

在ASP.NET中实现多文件上传
作者: 孟宪会 出自: 【孟宪会之精彩世界】 发布日期: 2003-5-20 23:41:07
--------------------------------------------------------------------------------

在以前的Web应用中,上传文件是个很麻烦的事,现在有了.NET,文件上传变得轻而易举。下面的这个例子实现了多文件上传功能。可以动态添加输入表单,上传的文件数量没有限制。代码如下:

MultiUpload.aspx

1@ Page Language="vb" AutoEventWireup="false" Codebehind="MultiUpload.aspx.vb"   
2Inherits="aspxWeb.MultiUpload" 
 1<html>
 2<head>
 3<title>多文件上传</title>
 4<script language="JavaScript">   
 5function addFile()   
 6{   
 7var str = '<INPUT type="file" size="50" NAME="File">'   
 8document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)   
 9}   
10</script>
11</head>
12<body>
13<form enctype="multipart/form-data" id="form1" method="post" runat="server">
14<center>
15<asp:label id="MyTitle" runat="server"></asp:label>
16<p id="MyFile"><input name="File" size="50" type="file"/></p>
17<p>
18<input onclick="addFile()" type="button" value="增加(Add)"/>
19<asp:button id="Upload" runat="server" text="上传"></asp:button>
20<input onclick="this.form.reset()" type="button" value="重置(ReSet)"/>
21</p>
22</center>
23<p align="center">
24<asp:label bordercolor="White" borderstyle="None" font-bold="True" font-names="宋体" font-size="9pt" id="strStatus" runat="server" width="500px"></asp:label>
25</p>
26</form>
27</body>
28</html>

后代码:MultiUpload.aspx.vb

Public Class MultiUpload
Inherits System.Web.UI.Page
Protected WithEvents Upload As System.Web.UI.WebControls.Button
Protected WithEvents MyTitle As System.Web.UI.WebControls.Label
Protected WithEvents strStatus As System.Web.UI.WebControls.Label

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

 1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent() 
 2
 3End Sub 
 4
 5Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init   
 6'CODEGEN: This method call is required by the Web Form Designer   
 7'Do not modify it using the code editor.   
 8InitializeComponent()   
 9End Sub 
10
11#End Region 
12
13Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load   
14MyTitle.Text = "<h3>多文件上传</h3>"   
15Upload.Text = "开始上传"   
16If (Me.IsPostBack) Then Me.SaveImages()   
17End Sub 
18
19Private Function SaveImages() As System.Boolean   
20'遍历File表单元素   
21Dim files As System.Web.HttpFileCollection = System.Web.HttpContext.Current.Request.Files 
22
23'状态信息   
24Dim strMsg As New System.Text.StringBuilder("上传的文件分别是:<hr color="red"/>")   
25Dim iFile As System.Int32   
26Try   
27For iFile = 0 To files.Count - 1   
28'检查文件扩展名字   
29Dim postedFile As System.Web.HttpPostedFile = files(iFile)   
30Dim fileName, fileExtension As System.String   
31fileName = System.IO.Path.GetFileName(postedFile.FileName)   
32If Not (fileName = String.Empty) Then   
33fileExtension = System.IO.Path.GetExtension(fileName)   
34strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br/>")   
35strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br/>")   
36strMsg.Append("上传文件的文件名:" + fileName + "<br/>")   
37strMsg.Append("上传文件的扩展名:" + fileExtension + "<br/><hr/>")   
38'可根据扩展名字的不同保存到不同的文件夹   
39postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/") + fileName)   
40End If   
41Next   
42strStatus.Text = strMsg.ToString()   
43Return True   
44Catch Ex As System.Exception   
45strStatus.Text = Ex.Message   
46Return False   
47End Try   
48End Function   
49End Class</system.diagnostics.debuggerstepthrough()>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus