使用FileUpload上传:

  1. protected void btnUpload_Click(object sender, EventArgs e)  
  2.         {  
  3.             if (FileUpload1.HasFile)  
  4.             {  
  5.                 /*通过文件扩展名判断文件类型*/ 
  6.                 string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);  
  7.                 if (fileExt != ".doc" && fileExt != ".docx" && fileExt != ".wps")  
  8.                 {  
  9.                     lblMsg.Text = "文件类型错误!应为:doc、docx或wps";  
  10.                     return;  
  11.                 }  
  12.                 /*限制文件大小*/ 
  13.                 if (FileUpload1.PostedFile.ContentLength > 100 * 1024)  
  14.                 {  
  15.                     lblMsg.Text = "文件应小于100KB";  
  16.                     return;  
  17.                 }  
  18.                 FileUpload1.SaveAs(Server.MapPath("虚拟路径") + FileUpload1.FileName);  
  19.                 lblMsg.Text = "文件上传成功!";  
  20.             }  
  21.             else 
  22.             {  
  23.                 lblMsg.Text = "未选择上传文件";  
  24.             }  
  25.         } 

文件下载:

  1. protected void btnDownload_Click(object sender, EventArgs e)  
  2.         {  
  3.             string fileName = "文件名.扩展名";  
  4.             FileInfo fileInfo = new FileInfo(Server.MapPath("虚拟路径") + fileName);  
  5.             Response.ContentType = "application/octet-stream";  
  6.             Response.AddHeader("Content-Disposition""attachment;FileName=" + HttpUtility.UrlEncode(fileName));  
  7.             /*这里注意,如果没有下面一句,很可能会造成“文件已损坏”等情况*/ 
  8.             Response.AddHeader("Content-Length", fileInfo.Length.ToString());  
  9.             Response.WriteFile(Server.MapPath("虚拟路径") + fileName);  
  10.         }