关于文件的上传。

asp.net里的代码:
None.gif          < form  id ="Form1"  method ="post"  runat ="server"  enctype ="multipart/form-data" >
None.gif            
< input  type ="file"  runat ="server"  id ="UploadImage"  name ="UploadImage" >
None.gif            
< asp:Button  id ="Button1"  runat ="server"  Text ="Button" ></ asp:Button >
None.gif            
< asp:Label  id ="lblMsg"  runat ="server"  ForeColor ="Red" ></ asp:Label >
None.gif        
</ form >
button的事件如下:
None.gif          private   void  Button1_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string all_filename,this_filename,ext_filename,new_filename;
InBlock.gif            
InBlock.gif            
if(UploadImage.PostedFile.ContentLength==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.lblMsg.Text="该文件不存在。";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                all_filename
=UploadImage.PostedFile.FileName;
InBlock.gif                
string[] a=all_filename.Split("\\".ToCharArray());
InBlock.gif                this_filename
=a[a.Length-1].ToString();
InBlock.gif                ext_filename
=this_filename.Remove(0,this_filename.LastIndexOf("."));
InBlock.gif                new_filename
=System.Guid.NewGuid()+ext_filename;
InBlock.gif
InBlock.gif                UploadImage.PostedFile.SaveAs(Server.MapPath(
"/sourcefile"+ @"\c1\" + new_filename);
InBlock.gif                
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }


以上是对单个文件的上传的方法,如果要同时实现多文件的上传。代码如下:
这是asp.net页面里的代码,注意新添加的file可以不需要id和name的
None.gif          < form  id ="Form1"  method ="post"  encType ="multipart/form-data"  runat ="server" >
None.gif            
< P >< input  id ="UploadImage"  type ="file"  name ="UploadImage"  runat ="server" >< br >
None.gif                
< input  type ="file"  runat ="server" >< br >
None.gif                
< input  type ="file"  runat ="server" ></ P >
None.gif            
< P > &nbsp;
None.gif                
< asp:Button  id ="Button1"  runat ="server"  Text ="Button" ></ asp:Button >
None.gif                
< asp:Label  id ="lblMsg"  runat ="server"  ForeColor ="Red" ></ asp:Label ></ P >
None.gif        
</ form >

c#代码部分
ExpandedBlockStart.gif ContractedBlock.gif          dot.gif {
InBlock.gif            
for(int i=0;i<Request.Files.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Web.HttpPostedFile myfile
=Request.Files[i];
InBlock.gif
InBlock.gif                
string all_filename,this_filename,ext_filename,new_filename;
InBlock.gif            
InBlock.gif                
if(myfile.ContentLength!=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    all_filename
=myfile.FileName;
InBlock.gif                    
string[] a=all_filename.Split("\\".ToCharArray());
InBlock.gif                    this_filename
=a[a.Length-1].ToString();
InBlock.gif                    ext_filename
=this_filename.Remove(0,this_filename.LastIndexOf("."));
InBlock.gif                    new_filename
=System.Guid.NewGuid()+ext_filename;
InBlock.gif
InBlock.gif                    myfile.SaveAs(Server.MapPath(
"/sourcefile"+ @"\c1\" + new_filename);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/songafeng/archive/2005/06/06/168801.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值