asp.net 2.0 上传控件的使用

试用了一下asp.net 2.0的上传控件,还是挺方便的。

分享代码如下:

None.gif      protected   void  Button1_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif            
判断文件大小#region 判断文件大小
InBlock.gif            
int intFileLength = this.FileUpload1.PostedFile.ContentLength;
InBlock.gif
InBlock.gif            
if (intFileLength > 50000)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.Label1.Text = "文件大于50k,不能上传";
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif            
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif            
判断保存的文件夹是否存在#region 判断保存的文件夹是否存在
InBlock.gif            
string strUpPath = @"upfile\" + System.DateTime.Now.ToShortDateString();// +@"\";
InBlock.gif
InBlock.gif            
//文件夹不存在的时候,创建文件夹
InBlock.gif
            if (!System.IO.Directory.Exists(Server.MapPath(strUpPath)))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.IO.Directory.CreateDirectory(Server.MapPath(strUpPath));
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
string strUrl = Server.MapPath(strUpPath + @"\" + this.FileUpload1.FileName);
ExpandedSubBlockEnd.gif            
#endregion

InBlock.gif            
InBlock.gif            
//上传文件
InBlock.gif
            this.FileUpload1.SaveAs(strUrl);
InBlock.gif
InBlock.gif            
this.Label1.Text = "文件上传成功";
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (System.Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Label1.Text = "文件上传失败:" + ex.Message;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

示例工程: /Files/heekui/WebUpLoad.rar
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值