现在的位置: 首页 > 综合 > 正文

FileUpload 上传多个文件

2012年10月10日 ⁄ 综合 ⁄ 共 2411字 ⁄ 字号 评论关闭
 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="i05630229(2).aspx.cs" Inherits="i05630229_2_" %>
 2 
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4 <html xmlns="http://www.w3.org/1999/xhtml">
 5 <head runat="server">
 6     <title>无标题页</title>
 7 </head>
 8 <body>
 9     <form id="form1" runat="server">
10         <div>
11             <asp:Panel ID="Panel1" runat="server" Height="106px" Width="389px">
12                 <asp:FileUpload ID="FileUpload1" runat="server" Width="313px" /></asp:Panel>
13             <asp:TextBox ID="tb_sum" runat="server" Width="40px"></asp:TextBox>
14             <asp:Button ID="btn_add" runat="server" Width="59px" Text="增加" OnClick="btn_add_Click" />
15             <asp:Button ID="btn_upfile" runat="server" Text="上传全部" OnClick="btn_upfile_Click" /></div>
16     </form>
17 </body>
18 </html>

 1 using System;
 2 using System.Data;
 3 using System.Configuration;
 4 using System.Collections;
 5 using System.Web;
 6 using System.Web.Security;
 7 using System.Web.UI;
 8 using System.Web.UI.WebControls;
 9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.IO;
12 
13 public partial class i05630229_2_ : System.Web.UI.Page
14 {
15     protected void Page_Load(object sender, EventArgs e)
16     {
17 
18     }
19     protected void btn_add_Click(object sender, EventArgs e)
20     {
21         //增加上传文件的个数
22         FileUpload fu;
23         for (int i = 0; i < Convert.ToInt32(tb_sum.Text); i++)
24         {
25             fu = new FileUpload();
26             fu.ID = "fu_" + i.ToString();
27             Panel1.Controls.Add(fu);
28         }
29 
30     }
31     protected void btn_upfile_Click(object sender, EventArgs e)
32     {
33         string f_name, f_size, f_type;
34         //申明几个变量
35         HttpFileCollection hfc = Request.Files;
36         //首先我们需要使用一个变量来获取到由客户端上传的文件集合
37         for (int i = 0; i < hfc.Count; i++)
38         {
39             //接下来循环这个集合
40             HttpPostedFile hpf = hfc[i];
41             //将每个文件实例化成可以直接访问的实体
42             f_name = Path.GetFileName(hpf.FileName);
43             f_size = hpf.ContentLength.ToString();
44             f_type = hpf.ContentType;
45             hpf.SaveAs(Server.MapPath("~/up/"+ f_name);   //当前目录下存在up文件夹
46             Response.Write(f_name + "<br/>" + f_size + "<br/>" + f_type + "<br/>");
47         }
48     }
49 }
50 

抱歉!评论已关闭.