.net使用FileUpLoad控件上传文件

单个文件的上传:
保存到上传服务器指定目录: FileUpload1.Save(Server.MapPath("/upfiles/upload/") +FileUpload1.FileName);

得到上传文件的文件名(含上传本地路径):FileUpload1.PostedFile.FileName;
得到上传文件的大小:FileUpload1.PostedFile.ContentLength;
得到上传文件上传类型:FileUpload1.PostedFile.ContentType;
得到上传文件扩展名:System.IO.Path.GetExtension(FileUpload1.FileName);
得到上传文件名:FileUpload1.FileName;
同时多个文件的上传:
   
方法是将 System.IO 类导入到 ASP.NET 页中,然后使用 HttpFileCollection 类捕获通过 Request 对象发送来的所有文件。该方法使您可以从一个页面上载所需数量的文件。

使用 HttpFileCollection 类和 Request.Files 属性使您可以控制从该页上载的所有文件。
(你可以在上传页面上放N个FileUpload控件)

得到上传的文件名:System.IO.Path.GetFileName(FileUpload1.FileName);//Request.Files得到的多部分MIME格式的由客户端上载的文件的集合都是包含上传本地完整路径的。

protected void Button1_Click(object sender, EventArgs e)
{
   string filepath = Server.MapPath("/upfiles/upload/") ;

HttpFileCollection uploadedFiles = Request.Files;       for (int i = 0; i < uploadedFiles.Count; i++)   {          HttpPostedFile userPostedFile = uploadedFiles[i];          try      {             if (userPostedFile.ContentLength > 0 )         {            Label1.Text += "File #" + (i+1) +                "";            Label1.Text += "File Content Type: " +                userPostedFile.ContentType + "";            Label1.Text += "File Size: " +                userPostedFile.ContentLength + "kb";            Label1.Text += "File Name: " +                userPostedFile.FileName + "";                userPostedFile.SaveAs(filepath + "//" +                System.IO.Path.GetFileName(userPostedFile.FileName));                Label1.Text += "Location where saved: " +                filepath + "//" +                System.IO.Path.GetFileName(userPostedFile.FileName) + "";         }          }       catch (Exception Ex)      {             Label1.Text += "Error: " + Ex.Message;          }       }    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值