.net 实现多文件上传

       今天用到了同时上传多个文件,自己写了段代码来实现这个功能。可以在其中限制文件大小,上传格式。但是没有做数量的限制。估计传多了也自然会失败吧。具体ContentType里对应的值,请参考我的另一篇文章 http://www.cnblogs.com/mgod/archive/2007/04/24/725200.html
.CS的代码如下

None.gif protected   bool  upMorefile()
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
InBlock.gif        
//遍历File表单元素
InBlock.gif
        System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
InBlock.gif        
//状态信息
InBlock.gif
        System.Text.StringBuilder strMsg = new System.Text.StringBuilder("成功上传的文件信息分别为:<hr color=red>");
InBlock.gif        
int fileCount;
InBlock.gif        
int filecount = files.Count;
InBlock.gif        
InBlock.gif       
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for (fileCount = 0; fileCount < files.Count; fileCount++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//定义访问客户端上传文件的对象
InBlock.gif
                System.Web.HttpPostedFile postedFile = files[fileCount];
InBlock.gif                
string FileType = postedFile.ContentType.ToString();//获取要上传的文件类型,验证文件头  
InBlock.gif

InBlock.gif                
string fileName, fileExtension;
InBlock.gif                
//取得上传得文件名
InBlock.gif
                fileName = System.IO.Path.GetFileName(postedFile.FileName);
InBlock.gif                
//取得文件的扩展名
InBlock.gif
                fileExtension = System.IO.Path.GetExtension(fileName);
InBlock.gif
InBlock.gif                
//在上传文件不为空的情况下,验证文件名以及大小是否符合,如果不符合则不允许上传
InBlock.gif
                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)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{//在这里通过检查文件头与文件名是否匹配 从而限制了文件上传类型  注:可上传的类型有TXT,ZIP,RAR,且大小只能为1M一下
InBlock.gif
                    
InBlock.gif                    
if (fileName != String.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        fileName 
= RandomFileName() + fileExtension;
InBlock.gif                        
InBlock.gif                        
//上传的文件信息
InBlock.gif
                        strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
InBlock.gif                        strMsg.Append(
"客户端文件地址:" + postedFile.FileName + "<br>");
InBlock.gif                        strMsg.Append(
"上传文件的文件名:" + fileName + "<br>");
InBlock.gif                        strMsg.Append(
"上传文件的大小为:" + postedFile.ContentLength + "字节<br>");
InBlock.gif                        strMsg.Append(
"上传文件的扩展名:" + fileExtension + "<br><hr color=red>");
InBlock.gif                        
//保存到指定的文件夹
InBlock.gif
                        postedFile.SaveAs(Server.MapPath("public_file/" + UserName + "/"+ fileName);
InBlock.gif                        fileName 
= "";
InBlock.gif
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    strStatus.Text
+= ""+(fileCount+1)+"个文件不符合要求<br/>  ";
InBlock.gif                    
ExpandedSubBlockEnd.gif                }

InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            strStatus.Text 
+= strMsg.ToString();
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (System.Exception error)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            strStatus.Text 
= error.Message;
InBlock.gif            
return false;
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif
None.gif    
protected   void  Upload_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        strStatus.Text 
= "";//讲提示信息清空
InBlock.gif
        upMorefile();//调用上传类
InBlock.gif

ExpandedBlockEnd.gif    }

None.gif
None.gif
public   string  RandomFileName()
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {//返回随机数的类
InBlock.gif
        string filename = "";
InBlock.gif        
string r1 = "";
InBlock.gif        
string r2 = "";
InBlock.gif        
string r4 = "";
InBlock.gif        Random random 
= new Random();
InBlock.gif        r1 
= ((char)random.Next(6590)).ToString();//大写字母
InBlock.gif
        r2 = ((char)random.Next(97122)).ToString();//小写字母        
InBlock.gif
        r4 = random.Next(10000999999).ToString();
InBlock.gif        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;
InBlock.gif
InBlock.gif        
return filename;
InBlock.gif
ExpandedBlockEnd.gif    }

None.gif

前台代码如下

ExpandedBlockStart.gif ContractedBlock.gif < script  language ="JavaScript" > dot.gif
InBlock.gif            
function addFileControl()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
var str = '<INPUT type="file" NAME="File">'
InBlock.gif                document.getElementById('FileCollection').insertAdjacentHTML(
"beforeEnd",str)
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif    
</ script >
None.gif    
None.gif
None.gif            
< asp:Panel  ID ="PanelFileManage"  runat ="server"  Width ="100%" >
None.gif        
None.gif        
< id ="FileCollection" >< INPUT  type ="file"  name ="File" > &nbsp; </ P >
None.gif                
< align ="center"  style ="color:Red" >
None.gif                    允许上传的类型为:ZIP,RAR,TXT,大小为1M以下
</ p >
None.gif            
< align ="center" >< input  onclick ="addFileControl()"  type ="button"  value ="增加(File)" >
None.gif                
< asp:button  id ="Upload"  Runat ="server"  Text ="上传"  Width ="56px"  OnClick ="Upload_Click" ></ asp:button >
None.gif                
< input  style ="WIDTH: 56px; HEIGHT: 24px"  onclick ="this.form.reset()"  type ="button"  value ="重置" >
None.gif            
</ P >
None.gif            
< align ="center" >< asp:label  id ="strStatus"  runat ="server"  BorderColor ="White"  BorderStyle ="None"  Width ="500px"
None.gif                    Font-Size
="9pt"  Font-Bold ="True"  Font-Names ="宋体" ></ asp:label ></ P >
None.gif        
None.gif    
</ asp:Panel >  

并且一定要注意在form中添加一个enctype项,才可以使用。如下:

None.gif < form  id ="form1"  runat ="server"  enctype ="multipart/form-data" >

转载于:https://www.cnblogs.com/mgod/archive/2007/04/24/725393.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值