技术资料 >> ASP.Net资料库 | 
                  | 
               
              | 
         
        
          
              
				
  
	| 
	搜索标签:
	文件上传 
	 | 
   
  
	| .net 实现多文件上传 | 
   
  
	| [阅读次数:1413次]  [发布时间:2011年8月11日] | 
   
  
	
	
	.CS的代码如下
  
 protected bool upMorefile()
     {
   
   //遍历File表单元素
   System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
   //状态信息
   System.Text.StringBuilder strMsg = new System.Text.StringBuilder("成功上传的文件信息分别为:<hr color=red>");
   int fileCount;
   int filecount = files.Count;
   
   
   try
     {
   for (fileCount = 0; fileCount < files.Count; fileCount++)
     {
   //定义访问客户端上传文件的对象
   System.Web.HttpPostedFile postedFile = files[fileCount];
   string FileType = postedFile.ContentType.ToString();//获取要上传的文件类型,验证文件头 
  
   string fileName, fileExtension;
   //取得上传得文件名
   fileName = System.IO.Path.GetFileName(postedFile.FileName);
   //取得文件的扩展名
   fileExtension = System.IO.Path.GetExtension(fileName);
  
   //在上传文件不为空的情况下,验证文件名以及大小是否符合,如果不符合则不允许上传
   if (((FileType == "text/plain" && fileExtension.ToLower() == ".txt") || (FileType == "application/x-zip-compressed" && fileExtension.ToLower() == ".zip") || (FileType == "application/octet-stream" && fileExtension.ToLower() == ".rar"))&&postedFile.ContentLength/1024<=1024)
     {//在这里通过检查文件头与文件名是否匹配 从而限制了文件上传类型 注:可上传的类型有TXT,ZIP,RAR,且大小只能为1M一下
   
   if (fileName != String.Empty)
     {
   fileName = RandomFileName() + fileExtension;
   
   //上传的文件信息
   strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
   strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
   strMsg.Append("上传文件的文件名:" + fileName + "<br>");
   strMsg.Append("上传文件的大小为:" + postedFile.ContentLength + "字节<br>");
   strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr color=red>");
   //保存到指定的文件夹
   postedFile.SaveAs(Server.MapPath("public_file/" + UserName + "/") + fileName);
   fileName = "";
  
   }
   }
   else
     {
   strStatus.Text+= "第"+(fileCount+1)+"个文件不符合要求<br/> ";
   
   }
   
   }
   
   strStatus.Text += strMsg.ToString();
   return true;
   }
   catch (System.Exception error)
     {
   strStatus.Text = error.Message;
   return false;
  
   }
   }
  
  
   protected void Upload_Click(object sender, EventArgs e)
     {
   strStatus.Text = "";//讲提示信息清空
   upMorefile();//调用上传类
  
   }
  
  public string RandomFileName()
     {//返回随机数的类
   string filename = "";
   string r1 = "";
   string r2 = "";
   string r4 = "";
   Random random = new Random();
   r1 = ((char)random.Next(65, 90)).ToString();//大写字母
   r2 = ((char)random.Next(97, 122)).ToString();//小写字母 
   r4 = random.Next(10000, 999999).ToString();
   filename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + r1 + r4 + r2 + r1 + r4 + r1;
  
   return filename;
  
   }
   前台代码如下
  
  <script language="JavaScript"> 
   function addFileControl()
     {
   var str = '<INPUT type="file" NAME="File">'
   document.getElementById('FileCollection').insertAdjacentHTML("beforeEnd",str)
   }
   </script>
   
  
   <asp:Panel ID="PanelFileManage" runat="server" Width="100%">
   
   <P id="FileCollection"><INPUT type="file" name="File"> </P>
   <p align="center" style="color:Red">
   允许上传的类型为:ZIP,RAR,TXT,大小为1M以下</p>
   <P align="center"><input onclick="addFileControl()" type="button" value="增加(File)">
   <asp:button id="Upload" Runat="server" Text="上传" Width="56px" OnClick="Upload_Click"></asp:button>
   <input style="WIDTH: 56px; HEIGHT: 24px" onclick="this.form.reset()" type="button" value="重置">
   </P>
   <P align="center"><asp:label id="strStatus" runat="server" BorderColor="White" BorderStyle="None" Width="500px"
   Font-Size="9pt" Font-Bold="True" Font-Names="宋体"></asp:label></P>
   
   </asp:Panel>  并且一定要注意在form中添加一个enctype项,才可以使用。如下:
  
 <form id="form1" runat="server" enctype="multipart/form-data"> 
	
  
	
	
  
	
	本页地址:
	[复制地址]
	
	
	 该页内容非本站原创 收藏自:http://www.cnblogs.com/mgod/archive/2007/04/24/725393.aspx
		 | 
   
  
    返回顶部   | 
   
 
 | 
               
              
              
                | 
 | 
               
			  
            | 
         
       
     | 
    
  
       推荐链接 | 
   
  
     | 
   
 
  
    
        
             最近更新 | 
            | 
         
        | 
   
  
     | 
   
 
  
    
        
             热门浏览 | 
            | 
         
        | 
   
  
     | 
   
 
 |