使用.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格式的由客户端上载的文件的集合都是包含上传本地完整路径的。

  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3.    string filepath = Server.MapPath("/upfiles/upload/") ;
  4. HttpFileCollection uploadedFiles = Request.Files;
  5.     
  6.    for (int i = 0; i < uploadedFiles.Count; i++)
  7.    {    
  8.       HttpPostedFile userPostedFile = uploadedFiles[i];
  9.     
  10.       try
  11.       {    
  12.          if (userPostedFile.ContentLength > 0 )
  13.          {
  14.             Label1.Text += "File #" + (i+1) + 
  15.                "";
  16.             Label1.Text += "File Content Type: " + 
  17.                userPostedFile.ContentType + "";
  18.             Label1.Text += "File Size: " + 
  19.                userPostedFile.ContentLength + "kb";
  20.             Label1.Text += "File Name: " + 
  21.                userPostedFile.FileName + "";
  22.     
  23.             userPostedFile.SaveAs(filepath + "//" + 
  24.                System.IO.Path.GetFileName(userPostedFile.FileName));
  25.     
  26.             Label1.Text += "Location where saved: " + 
  27.                filepath + "//" + 
  28.                System.IO.Path.GetFileName(userPostedFile.FileName) + "";
  29.          }    
  30.       } 
  31.       catch (Exception Ex)
  32.       {    
  33.          Label1.Text += "Error: " + Ex.Message;    
  34.       }    
  35.    }    
  36. }

以下代码是在FileUpload控件里选择了要上传的文件,然后点"上传"按钮实现上传的代码.

  1. protected void Btn_FileUpload_Click(object sender, EventArgs e) 
  2.     { 
  3.         string filePath = string.Empty; 
  4.         FileInfo fi; 
  5.         if (FileUpload1.HasFile && FileUpload1.FileName.ToUpper().EndsWith(".LAB")) 
  6.         { 
  7.             try 
  8.             { 
  9.                 if (RadioButtonList1.Items[0].Selected) 
  10.                 { 
  11.                     filePath = @"//192.168.0.95/lab/" + FileUpload1.FileName; 
  12.                 } 
  13.                 else if (RadioButtonList1.Items[1].Selected) 
  14.                 { 
  15.                     filePath = @"D:/lab/" + FileUpload1.FileName; 
  16.                 } 
  17.                 //Response.Write(" <script>window.alert('" + filePath + "-FULLNAME:" + FileUpload1.PostedFile.FileName + "') </script>"); 
  18.                 //fi = new FileInfo(FileUpload1.PostedFile.FileName); 
  19.                 //fi.CopyTo(filePath); 
  20.                 FileUpload1.SaveAs(filePath); 
  21.                 Response.Write(" <script>alert('文件上传成功!') </script>"); 
  22.                 GridView1.Rows[GridView1.EditIndex].Cells[6].Text = FileUpload1.FileName; 
  23.             } 
  24.             catch (Exception ex) 
  25.             { 
  26.                 Response.Write(" <script>alert('" + ex.Message + "') </script>"); 
  27.             } 
  28.         } 
  29.         else 
  30.         { 
  31.             Response.Write(" <script>alert('请选择CodeSoft模板文件!') </script>"); 
  32.         } 
  33.     } 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值