动态,同时上传多个文件

我们有时候作上传功能的时候会让用户上传多个图片或文件,一个一个上传吧有点麻烦。如果做成死的,一次上次,三个,或是五个的,有时候还不够用,这就很烦了。下面这种方法,在上传的地方加一个按钮,如果用户想上传几个就点几次,这样就会出现多个上传框,让他选择,好了,不多说了。下面是代码
前台代码
  1. <html xmlns="http://www.w3.org/1999/xhtml" >
  2. <head runat="server">
  3.     <title>无标题页</title>
  4.     <script language="javascript" type="text/javascript">
  5.        function addFile(max)
  6.        {
  7.           var file = document.getElementsByName("File");
  8.           alert(file.length);
  9.           if(file.length==1 && file[0].disabled==true)
  10.           {
  11.               file[0].disabled = false;
  12.               return;
  13.           }
  14.           
  15.           if(file.length<max)
  16.           {
  17.               
  18.               var fileButton = '<br /><input type="file" size="50" name="File" />';
  19.               alert(fileButton);
  20.               document.getElementById("FileList").insertAdjacentHTML("beforeEnd",fileButton);
  21.           }
  22.        }
  23.     </script>
  24. </head>
  25. <body>
  26.     <form id="form1" runat="server" enctype="multipart/form-data">
  27.     <p id="FileList">
  28.        <input type="file" disabled="disabled" size="50" name="File" />
  29.     </p>
  30.        <input type="button" value='增加一个文件' onclick="addFile(<%=MaxFileCounts%>)" />
  31.         <br />
  32.         <br />
  33.         <br />
  34.         <asp:Button ID="Button1" runat="server" Text="添加" OnClick="Button1_Click" />
  35.         <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
  36.     </form>
  37. </body>
  38. </html>
这里有一点要注意的是<input type="button" value='增加一个文件' onclick="addFile(<%=MaxFileCounts%>)" />这里面的MaxFileCounts。这个是用户上传最大数的一个限制。你可以在这里写成死值,也可以在CS进行配置,主要看你的需求。

下面是cs文件了
  1.        这里的MaxFileCount是在我配置文件写的,你可以根据你的情况写。
  2.        public int MaxFileCounts = MaxFileCount;     
  3.         protected void Page_Load(object sender, EventArgs e)
  4.         {
  5.         }
  6.         protected void Button1_Click(object sender, EventArgs e)
  7.         {
  8.             HttpFileCollection fileList = HttpContext.Current.Request.Files;
  9.             if (fileList == null)
  10.             {
  11.                 return;
  12.             }
  13.             FileImage file = new FileImage();
  14.             try
  15.             {
  16.                 for (int i = 0; i < fileList.Count; i++)
  17.                 {
  18.                     HttpPostedFile postedFile = fileList[i];
  19.                     if (postedFile == null)
  20.                         continue;
  21.                     string fileName = Path.GetFileNameWithoutExtension(postedFile.FileName);
  22.                     string extension = Path.GetExtension(postedFile.FileName);
  23.                     if (string.IsNullOrEmpty(extension) == true)
  24.                         continue;
  25.                     bool flag = false;
  26.                     foreach (string ext in AllowFileList)
  27.                     {
  28.                         if (ext == extension.ToLower())
  29.                         {
  30.                             flag = true;
  31.                         }
  32.                     }
  33.                     if (flag == false)
  34.                         continue;
  35.                     string storeUrl = DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + extension.ToString();
  36.                     string Url = storeFilePath + storeUrl;
  37.                     string fullPath = Server.MapPath(Url);
  38.                     postedFile.SaveAs(fullPath);
  39.                     Hashtable ht = new Hashtable();
  40.                     ht.Add("Title",fileName);
  41.                     ht.Add("imgUrl",storeUrl);
  42.                     ht.Add("imgType",postedFile.ContentType);
  43.                     ht.Add("imgSize",postedFile.ContentLength);
  44.                     file.insertImage(ht);//这里是我的添加语句,你可写成你自己的。
  45.                 }
  46.             }
  47.             catch (Exception ex)
  48.             {
  49.                 this.Label1.Text = ex.Message;
  50.             }
  51.         }

好了,试一下吧,感谢还是很不错的



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值