zoukankan      html  css  js  c++  java
  • ORM映射框架总结文件下载

    本段代码提供了asp.net 中的各种文件下载方式。

    代码
      1 /**
      2  * 日期:2009-3-13
      3  * 作者:
      4  * 功能:下载文件
      5  * */
      6 
      7 using System;
      8 using System.Configuration;
      9 using System.Data;
     10 using System.Linq;
     11 using System.Web;
     12 using System.Web.Security;
     13 using System.Web.UI;
     14 using System.Web.UI.HtmlControls;
     15 using System.Web.UI.WebControls;
     16 using System.Web.UI.WebControls.WebParts;
     17 using System.Xml.Linq;
     18 using System.Web.Services;
     19 using System.IO;
     20 using CommonData.Exceptions;
     21 
     22 namespace CommonData.DownLoad
     23 {
     24     public partial class Down
     25     {
     26 
     27         #region(下载相关属性)
     28         private string _downFilePath;
     29 
     30         /// <summary>
     31         /// 下载文件路径
     32         /// </summary>
     33         public string DownFilePath
     34         {
     35             get { return _downFilePath; }
     36             set { _downFilePath = value; }
     37         }
     38 
     39         private string _fileName;
     40 
     41         /// <summary>
     42         /// 下载的文件名称
     43         /// </summary>
     44         private string FileName
     45         {
     46             get { return this._downFilePath.Substring(this._downFilePath.LastIndexOf("\\"+ 1); }
     47             set { _fileName = value; }
     48         }
     49 
     50         private string _saveName;
     51 
     52         /// <summary>
     53         /// 下载保存文件名称
     54         /// </summary>
     55         public string SaveName
     56         {
     57             get { return _saveName; }
     58             set { _saveName = value; }
     59         }
     60         #endregion
     61 
     62         #region (构造方法)
     63         /// <summary>
     64         /// 无参数构造方法
     65         /// </summary>
     66         public Down()
     67         {
     68         }
     69 
     70         /// <summary>
     71         /// 有参数构造方法
     72         /// </summary>
     73         /// <param name="downFilePath">下载文件的物理路径</param>
     74         public Down(string downFilePath)
     75         {
     76             this._downFilePath = downFilePath;
     77         }
     78 
     79         /// <summary>
     80         /// 有参数的构造方法
     81         /// </summary>
     82         /// <param name="downFilePath">下载文件的物理路径</param>
     83         /// <param name="saveName">下载保存文件的名称</param>
     84         public Down(string downFilePath, string saveName)
     85         {
     86             this._downFilePath = downFilePath;
     87             this._saveName = saveName;
     88         }
     89         #endregion
     90 
     91 
     92 
     93         #region(TransmitFile)
     94         /// <summary>
     95         /// 下载超过400mb的文件(TransmitFile)
     96         /// </summary>
     97         /// <param name="filepath">下载文件的物理路径</param>
     98         /// <param name="saveName">下载保存的文件名称</param>
     99         /// <returns></returns>
    100         public void TransmitFile(string filepath, string saveName)
    101         {
    102             this._downFilePath = filepath;
    103             if (string.IsNullOrEmpty(saveName))
    104             {
    105                 this._saveName = this.FileName;
    106             }
    107             else
    108             {
    109                 this._saveName = saveName;
    110             }
    111             if (!string.IsNullOrEmpty(this.DownFilePath))
    112             {
    113                 if (File.Exists(this._downFilePath))
    114                 {
    115                     System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
    116                     System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition""attachment;filename=" + System.Web.HttpUtility.UrlEncode(this.SaveName, System.Text.Encoding.UTF8));
    117                     System.Web.HttpContext.Current.Response.TransmitFile(this._downFilePath);
    118                 }
    119                 else
    120                 {
    121                     throw new CommonFileException("下载的文件已经被删除!");
    122                 }
    123             }
    124             else
    125             {
    126                 throw new CommonFileException("未提供可供下载的文件路径");
    127             }
    128         }
    129 
    130         /// <summary>
    131         /// 下载超过400mb的文件(TransmitFile)
    132         /// </summary>
    133         /// <param name="filepath">下载文件的物理路径</param>
    134         public void TransmitFile(string filepath)
    135         {
    136             this.TransmitFile(filepath, "");
    137         }
    138 
    139         /// <summary>
    140         /// 下载超过400mb的文件(TransmitFile)
    141         /// </summary>
    142         /// <returns></returns>
    143         public void TransmitFile()
    144         {
    145             this.TransmitFile(this._downFilePath, this.SaveName);
    146         }
    147         #endregion
    148 
    149 
    150         #region(WriteFile)
    151         /// <summary>
    152         /// WriteFile 
    153         /// </summary>
    154         /// <param name="filepath">下载文件的物理路径</param>
    155         /// <param name="saveName">下载保存的文件名称</param>
    156         public void WriteFile(string filepath, string saveName)
    157         {
    158             this._downFilePath = filepath;
    159             if (string.IsNullOrEmpty(saveName))
    160             {
    161                 this._saveName = this.FileName;
    162             }
    163             else
    164             {
    165                 this._saveName = saveName;
    166             }
    167             if (!string.IsNullOrEmpty(this.DownFilePath))
    168             {
    169                 if (File.Exists(this.DownFilePath))
    170                 {
    171                     FileInfo fileInfo = new FileInfo(this._downFilePath);
    172                     System.Web.HttpContext.Current.Response.Clear();
    173                     System.Web.HttpContext.Current.Response.ClearContent();
    174                     System.Web.HttpContext.Current.Response.ClearHeaders();
    175                     System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition""attachment;filename=" + HttpUtility.UrlPathEncode(this._saveName));
    176                     System.Web.HttpContext.Current.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
    177                     System.Web.HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding""binary");
    178                     System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
    179                     System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
    180                     System.Web.HttpContext.Current.Response.WriteFile(fileInfo.FullName);
    181                     System.Web.HttpContext.Current.Response.Flush();
    182                     System.Web.HttpContext.Current.Response.End();
    183                 }
    184                 else
    185                 {
    186                     throw new CommonFileException("下载的文件已经被移除!");
    187                 }
    188             }
    189             else
    190             {
    191                 throw new CommonFileException("未提供可供下载的文件路径!");
    192             }
    193         }
    194 
    195         /// <summary>
    196         /// WriteFile
    197         /// </summary>
    198         /// <param name="filepath">下载文件的物理路径</param>
    199         public void WriteFile(string filepath)
    200         {
    201             this.WriteFile(filepath, "");
    202         }
    203 
    204 
    205         /// <summary>
    206         /// WriteFile
    207         /// </summary>
    208         public void WriteFile()
    209         {
    210             this.WriteFile(this._downFilePath, this._saveName);
    211         }
    212 
    213         #endregion
    214 
    215         #region(WriteFile分块下载)
    216         /// <summary>
    217         /// WriteFile分块下载
    218         /// </summary>
    219         /// <param name="filepath">下载文件的物理路径</param>
    220         /// <param name="saveName">下载保存文件的名称</param>
    221         public void WriteFilePart(string filepath, string saveName)
    222         {
    223             this._downFilePath = filepath;
    224             if (string.IsNullOrEmpty(saveName))
    225             {
    226                 this._saveName = this.FileName;
    227             }
    228             else
    229             {
    230                 this._saveName = saveName;
    231             }
    232 
    233             if (!string.IsNullOrEmpty(this.DownFilePath))
    234             {
    235                 if (File.Exists(this.DownFilePath))
    236                 {
    237                     const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力 
    238                     byte[] buffer = new byte[ChunkSize];
    239 
    240                     System.Web.HttpContext.Current.Response.Clear();
    241                     FileStream iStream = File.OpenRead(this.DownFilePath);
    242                     long dataLengthToRead = iStream.Length;//获取下载的文件总大小 
    243                     System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
    244                     System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition""attachment; filename=" + HttpUtility.UrlEncode(this.SaveName));
    245                     while (dataLengthToRead > 0 && System.Web.HttpContext.Current.Response.IsClientConnected)
    246                     {
    247                         int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小 
    248                         System.Web.HttpContext.Current.Response.OutputStream.Write(buffer, 0, lengthRead);
    249                         System.Web.HttpContext.Current.Response.Flush();
    250                         dataLengthToRead = dataLengthToRead - lengthRead;
    251                     }
    252                     System.Web.HttpContext.Current.Response.Close();
    253 
    254                 }
    255                 else
    256                 {
    257                     throw new CommonFileException("下载的文件已经被移除!");
    258                 }
    259             }
    260             else
    261             {
    262                 throw new CommonFileException("未提供可供下载的文件路径!");
    263             }
    264         }
    265 
    266         /// <summary>
    267         /// WriteFile分块下载
    268         /// </summary>
    269         /// <param name="filepath">下载文件的物理路径</param>
    270         public void WriteFilePart(string filepath)
    271         {
    272             this.WriteFilePart(filepath, "");
    273         }
    274 
    275         /// <summary>
    276         /// WriteFile分块下载
    277         /// </summary>
    278         public void WriteFilePart()
    279         {
    280             this.WriteFilePart(this.DownFilePath, this.SaveName);
    281         }
    282         #endregion
    283 
    284 
    285         #region(以流的方式下载)
    286         /// <summary>
    287         /// 以流的方式下载
    288         /// </summary>
    289         /// <param name="filepath">下载文件的物理路径</param>
    290         /// <param name="saveName">下载保存文件的名称</param>
    291         public void DownStream(string filepath, string saveName)
    292         {
    293             this._downFilePath = filepath;
    294             if (string.IsNullOrEmpty(saveName))
    295             {
    296                 this._saveName = this.FileName;
    297             }
    298             else
    299             {
    300                 this._saveName = saveName;
    301             }
    302             if (!string.IsNullOrEmpty(this.DownFilePath))
    303             {
    304                 if (File.Exists(this.DownFilePath))
    305                 {
    306                     //以字符流的形式下载文件 
    307                     FileStream fs = new FileStream(this.DownFilePath, FileMode.Open);
    308                     byte[] bytes = new byte[(int)fs.Length];
    309                     fs.Read(bytes, 0, bytes.Length);
    310                     fs.Close();
    311                     System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
    312                     //通知浏览器下载文件而不是打开 
    313                     System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition""attachment; filename=" + HttpUtility.UrlEncode(this.SaveName, System.Text.Encoding.UTF8));
    314                     System.Web.HttpContext.Current.Response.BinaryWrite(bytes);
    315                     System.Web.HttpContext.Current.Response.Flush();
    316                     System.Web.HttpContext.Current.Response.End();
    317                 }
    318                 else
    319                 {
    320                     throw new CommonFileException("下载的文件已经被移除!");
    321                 }
    322             }
    323             else
    324             {
    325                 throw new CommonFileException("未提供可供下载的文件路径!");
    326             }
    327 
    328         }
    329 
    330         /// <summary>
    331         /// 以流的方式下载
    332         /// </summary>
    333         /// <param name="filepath">下载文件的物理路径</param>
    334         public void DownStream(string filepath)
    335         {
    336             this.DownStream(filepath, "");
    337         }
    338 
    339         /// <summary>
    340         /// 以流的方式下载
    341         /// </summary>
    342         public void DownStream()
    343         {
    344             this.DownStream(this.DownFilePath, this.SaveName);
    345         }
    346         #endregion
    347 
    348         #region(下载一个动态产生的文本文件)
    349         /// <summary>
    350         /// 下载一个动态产生的文本文件
    351         /// </summary>
    352         /// <param name="webForm">父页面对象</param>
    353         /// <param name="saveName">下载保存文件的名称</param>
    354         /// <param name="fileBody">下载文件的内容</param>
    355         public void DownloadFile(System.Web.UI.Page webForm, string saveName, string fileBody)
    356         {
    357             this._saveName = saveName;
    358             webForm.Response.ClearHeaders();
    359             webForm.Response.Clear();
    360             webForm.Response.Expires = 0;
    361             webForm.Response.Buffer = true;
    362             webForm.Response.AddHeader("Accept-Language""zh-tw");
    363             //'文件名称
    364             webForm.Response.AddHeader("content-disposition""attachment; filename='" + System.Web.HttpUtility.UrlEncode(this.SaveName, System.Text.Encoding.UTF8) + "'");
    365             webForm.Response.ContentType = "Application/octet-stream";
    366             //'文件内容
    367             webForm.Response.Write(fileBody);//-----------
    368             webForm.Response.End();
    369         }
    370         #endregion
    371 
    372         /// <summary>
    373         /// 下载一个服务端存在文件文本文件
    374         /// </summary>
    375         /// <param name="webForm">父页面对象</param>
    376         /// <param name="saveName">下载保存文件的名称</param>
    377         /// <param name="filepath">下载文件路径</param>
    378         [Obsolete]
    379         private void DownloadFileByFilePath(System.Web.UI.Page webForm, string saveName, string filepath)
    380         {
    381             this._downFilePath = filepath;
    382             if (string.IsNullOrEmpty(saveName))
    383             {
    384                 this._saveName = this.FileName;
    385             }
    386             else
    387             {
    388                 this._saveName = saveName;
    389             }
    390 
    391             if (!string.IsNullOrEmpty(this.DownFilePath))
    392             {
    393                 if (File.Exists(this.DownFilePath))
    394                 {
    395                     webForm.Response.ClearHeaders();
    396                     webForm.Response.Clear();
    397                     webForm.Response.Expires = 0;
    398                     webForm.Response.Buffer = true;
    399                     webForm.Response.AddHeader("Accept-Language""zh-tw");
    400                     //文件名称
    401                     webForm.Response.AddHeader("content-disposition""attachment; filename=" + System.Web.HttpUtility.UrlEncode(this.SaveName, System.Text.Encoding.UTF8));
    402                     webForm.Response.ContentType = "Application/octet-stream";
    403                     //文件内容
    404                     webForm.Response.Write(System.IO.File.ReadAllBytes(this.DownFilePath).ToString());//---------
    405                     webForm.Response.End();
    406                 }
    407                 else
    408                 {
    409                     throw new CommonFileException("下载的文件已经被移除!");
    410                 }
    411             }
    412             else
    413             {
    414                 throw new CommonFileException("未提供可供下载的文件路径!");
    415 
    416             }
    417         }
    418 
    419     }
    420 }
    421 
  • 相关阅读:
    浅谈独立使用NDK编译库文件(Android)
    Ubuntu 13.04 安装使用clang
    一道TOPK问题
    exp-00091 oracle错误的解决办法
    win32多线程程序设计笔记(第四章下)
    数据库索引的实现原理 (转)
    [置顶] linux常用命令手册
    ZigBee研究之旅(二)
    java中length,length(),size()区别
    浅谈href=#与href=javascript:void(0)的区别
  • 原文地址:https://www.cnblogs.com/qingyuan/p/1637823.html
Copyright © 2011-2022 走看看