asp.net 上传下载

ASP.NET上传下载文件

上传文件:

using System;  
  1. using System.Collections.Generic;  
  2. using System.Linq;  
  3. using System.Web;  
  4. using System.Web.UI;  
  5. using System.Web.UI.WebControls;  
  6. using System.IO;   
    #region 上传文件  
    /// <summary>   
  1. /// 上传文件   
  2. /// </summary>   
  3. /// <param name="UpLoadFilePath">上传位置</param>  
  4. /// <param name="strTime">当前时间(加时间重命名文件,防止重复)</param>  
  5. /// <param name="upload">上传控件</param>   
  6. private void UploadMethod(String UpLoadFilePath, string strTime,FileUpload upload)  
  7. {  
  8.     bool fileOK = false;  
  9.   
  10.     //文件的上传路径   
  11.     string path = Server.MapPath(UpLoadFilePath);  
  12.   
  13.     //判断上传文件夹是否存在,若不存在,则创建   
  14.     if (!Directory.Exists(path))  
  15.     {  
  16.         //创建文件夹    
  17.         Directory.CreateDirectory(path);  
  18.     }  
  19.     //如果选择了文件则执行   
  20.     if (upload.HasFile)  
  21.     {  
  22.          
  23.         //获取上传文件的类型   
  24.         string fileExtesion = System.IO.Path.GetExtension(upload.FileName).ToLower();  
  25.         //获取没有扩展名的文件名   
  26.         string result = System.IO.Path.GetFileNameWithoutExtension(upload.FileName);  
  27.   
  28.         //允许上传的类型   
  29.         string[] allowExtesions = { ".doc"".xls"".rar"".zip"".ppt" };  
  30.         for (int i = 0; i < allowExtesions.Length; i++)  
  31.         {  
  32.             if (fileExtesion == allowExtesions[i])  
  33.             {  
  34.                 //文件格式正确 允许上传   
  35.                 fileOK = true;  
  36.             }  
  37.         }  
  38.   
  39.         if (fileOK)  
  40.         {  
  41.             try  
  42.             {  
  43.                string  newname = result + strTime + fileExtesion;  
  44.                upload.PostedFile.SaveAs(path + newname);  
  45.                Session["filename"] = newname;  
  46.             }  
  47.             catch (Exception)  
  48.             {  
  49.                 Page.ClientScript.RegisterStartupScript(Page.GetType(), "message""<script language='javascript' defer>alert('上传失败!');</script>");  
  50.             }  
  51.         }  
  52.         else  
  53.         {  
  54.             Page.ClientScript.RegisterStartupScript(Page.GetType(), "message""<script language='javascript' defer>alert('上传失败!');</script>");  
  55.         }  
  56.     }  
  57.     else  
  58.     {  
  59.         //尚未选择文件   
  60.         Page.ClientScript.RegisterStartupScript(Page.GetType(), "message""<script language='javascript' defer>alert('尚未选择任何文件,请选择文件!');</script>");  
  61.         return;  
  62.     }  
  63. }  
  64. #endregion  

下载文件:

 

 
/// <summary>   
  1.         /// 下载文件   
  2.         /// </summary>   
  3.         /// <param name="downloadfilepath">文件路径</param>  
  4.         private void DownloadMethod(string downloadfilepath)  
  5.         {  
  6.             string path = Server.MapPath(downloadfilepath);  
  7.   
  8.             Regex re = new Regex(@"\w*\.\w*");  
  9.             string st = re.Match(path).Value.ToString();  
  10.             string fileName = st; //客户端保存的文件名  
  11.   
  12.             FileInfo fileInfo = new FileInfo(path);  
  13.             Response.Clear();  
  14.             Response.ClearContent();  
  15.             Response.ClearHeaders();  
  16.             Response.AddHeader("Content-Disposition""attachment;filename=" + fileName);  
  17.             Response.AddHeader("Content-Length", fileInfo.Length.ToString());  
  18.             Response.AddHeader("Content-Transfer-Encoding""binary");  
  19.             Response.ContentType = "application/octet-stream";  
  20.             Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");  
  21.             Response.WriteFile(fileInfo.FullName);  
  22.             Response.Flush();  
  23.             Response.End();  
  24.         }  

调用:

    UploadMethod(DocPath,strTime,fileUpload);

    DownloadMethod(filePath);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值