当前位置:首页>编程日记>正文

[转]asp.net文件下载方法...

TransmitFile实现下载    

protected void Button1_Click(object sender, EventArgs e)

     {
         /* 
         微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite 
         下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。 
         代码如下: 
         */ 
         Response.ContentType = "application/x-zip-compressed";
         Response.AddHeader("Content-Disposition", "attachment;filename=z.zip");
         string filename = Server.MapPath("DownLoad/z.zip");
         Response.TransmitFile(filename);
     }
     //WriteFile实现下载 
     protected void Button2_Click(object sender, EventArgs e)
     {
         /* 
         using System.IO;
         */
         string fileName = "asd.txt";//客户端保存的文件名 
         string filePath = Server.MapPath("DownLoad/aaa.txt");//路径
         FileInfo fileInfo = new FileInfo(filePath);
         Response.Clear();
         Response.ClearContent();
         Response.ClearHeaders();
         Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
         Response.AddHeader("Content-Length", fileInfo.Length.ToString());
         Response.AddHeader("Content-Transfer-Encoding", "binary");
         Response.ContentType = "application/octet-stream";
         Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
         Response.WriteFile(fileInfo.FullName);
         Response.Flush();
         Response.End();
     }
     //WriteFile分块下载 
     protected void Button3_Click(object sender, EventArgs e)
     {
         string fileName = "aaa.txt";//客户端保存的文件名 
         string filePath = Server.MapPath("DownLoad/aaa.txt");//路径
         System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
         if (fileInfo.Exists == true)
         {
             const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力 
             byte[] buffer = new byte[ChunkSize];
             Response.Clear();
             System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
             long dataLengthToRead = iStream.Length;//获取下载的文件总大小 
             Response.ContentType = "application/octet-stream";
             Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
             while (dataLengthToRead > 0 && Response.IsClientConnected)
             {
                 int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小 
                 Response.OutputStream.Write(buffer, 0, lengthRead);
                 Response.Flush();
                 dataLengthToRead = dataLengthToRead - lengthRead;
             }
             Response.Close();
         }
     }
     //流方式下载 
     protected void Button4_Click(object sender, EventArgs e)
     {
         string fileName = "aaa.txt";//客户端保存的文件名 
         string filePath = Server.MapPath("DownLoad/aaa.txt");//路径
         //以字符流的形式下载文件 
         FileStream fs = new FileStream(filePath, FileMode.Open);
         byte[] bytes = new byte[(int)fs.Length];
         fs.Read(bytes, 0, bytes.Length);
         fs.Close();
         Response.ContentType = "application/octet-stream";
         //通知浏览器下载文件而不是打开 
         Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
         Response.BinaryWrite(bytes);
         Response.Flush();
         Response.End();
     }
//----------------------------------------------------------

public void DownloadFile( System.Web.UI.Page WebForm,String FileNameWhenUserDownload ,String FileBody )
{
  WebForm.Response.ClearHeaders();
  WebForm.Response.Clear();
  WebForm.Response.Expires = 0;
  WebForm.Response.Buffer = true;
  WebForm.Response.AddHeader("Accept-Language", "zh-tw");
  //'文件名称
  WebForm.Response.AddHeader("content-disposition", "attachment; filename='"+System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8)+"'");
  WebForm.Response.ContentType = "Application/octet-stream";
  //'文件内容
  WebForm.Response.Write(FileBody);//-----------
     WebForm.Response.End();
}
//上面这段代码是下载一个动态产生的文本文件,若这个文件已经存在于服务器端的实体路径,则可以通过下面的函数:
public void DownloadFileByFilePath( System.Web.UI.Page WebForm,String FileNameWhenUserDownload ,String FilePath )
{
  WebForm.Response.ClearHeaders();
  WebForm.Response.Clear();
  WebForm.Response.Expires = 0;
     WebForm.Response.Buffer = true;
  WebForm.Response.AddHeader("Accept-Language", "zh-tw");
  //文件名称
  WebForm.Response.AddHeader("content-disposition", "attachment; filename='" + System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8) +"'" );
  WebForm.Response.ContentType = "Application/octet-stream";
  //文件内容
  WebForm.Response.Write(System.IO.File.ReadAllBytes(FilePath));//---------
  WebForm.Response.End();
}

http://www.coolblog.cn/news/8d03635133733456.html

相关文章:

  • asp多表查询并显示_SpringBoot系列(五):SpringBoot整合Mybatis实现多表关联查询
  • s7day2学习记录
  • 【求锤得锤的故事】Redis锁从面试连环炮聊到神仙打架。
  • 矿Spring入门Demo
  • 拼音怎么写_老师:不会写的字用圈代替,看到孩子试卷,网友:人才
  • Linux 实时流量监测(iptraf中文图解)
  • Win10 + Python + GPU版MXNet + VS2015 + RTools + R配置
  • 美颜
  • shell访问php文件夹,Shell获取某目录下所有文件夹的名称
  • 如何优雅的实现 Spring Boot 接口参数加密解密?
  • LeCun亲授的深度学习入门课:从飞行器的发明到卷积神经网络
  • Mac原生Terminal快速登录ssh
  • java受保护的数据与_Javascript类定义语法,私有成员、受保护成员、静态成员等介绍...
  • mysql commit 机制_1024MySQL事物提交机制
  • 支撑微博千亿调用的轻量级RPC框架:Motan
  • jquery 使用小技巧
  • 2019-9
  • 法拉利虚拟学院2010 服务器,法拉利虚拟学院2010
  • vscode pylint 错误_将实际未错误的py库添加到pylint白名单
  • 科学计算工具NumPy(3):ndarray的元素处理
  • 工程师在工作电脑存 64G 不雅文件,被公司开除后索赔 41 万,结果…
  • linux批量创建用户和密码
  • newinsets用法java_Java XYPlot.setInsets方法代碼示例
  • js常用阻止冒泡事件
  • 气泡图在开源监控工具中的应用效果
  • 各类型土地利用图例_划重点!国土空间总体规划——土地利用
  • php 启动服务器监听
  • dubbo简单示例
  • 【设计模式】 模式PK:策略模式VS状态模式
  • [iptables]Redhat 7.2下使用iptables实现NAT
  • Ubuntu13.10:[3]如何开启SSH SERVER服务
  • CSS小技巧——CSS滚动条美化
  • JS实现-页面数据无限加载
  • 阿里巴巴分布式服务框架 Dubbo
  • 最新DOS大全
  • Django View(视图系统)
  • 阿里大鱼.net core 发送短信
  • 程序员入错行怎么办?
  • 两张超级大表join优化
  • 第九天函数
  • Linux软件安装-----apache安装
  • HDU 5988 最小费用流
  • Sorenson Capital:值得投资的 5 种 AI 技术
  • 《看透springmvc源码分析与实践》读书笔记一
  • 正式开课!如何学习相机模型与标定?(单目+双目+鱼眼+深度相机)
  • Arm芯片的新革命在缓缓上演
  • nagios自写插件—check_file
  • python3 错误 Max retries exceeded with url 解决方法
  • 行为模式之Template Method模式
  • 通过Spark进行ALS离线和Stream实时推荐