.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.Rea}dAllBytes(FilePath));//---------   WebForm.Response.End();

.net中下载文件的方法(转)的更多相关文章

  1. Javaweb向服务器上传文件以及从服务器下载文件的方法

    先导入jar包 点击下载 commons-fileupload是Apache开发的一款专门用来处理上传的工具,它的作用就是可以从request对象中解析出,用户发送的请求参数和上传文件的流. comm ...

  2. linux 从百度网盘下载文件的方法

    linux 从百度网盘下载文件的方法 发表于2015 年 月 日由shenwang 方法1.wget wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括 ...

  3. VSTO学习笔记(四)从SharePoint 2010中下载文件

    原文:VSTO学习笔记(四)从SharePoint 2010中下载文件 上一次我们开发了一个简单的64位COM加载项,虽然功能很简单,但是包括了开发一个64位COM加载项的大部分过程.本次我们来给CO ...

  4. js中使用showModelDialog中下载文件的时候,闪一下后无法下载

    在js中使用showModelDialog中下载文件的时候,会因为showModelDialog自动设置target为_self导致下载文件“只会闪一下”就消失掉 在吧target设置为_blank后 ...

  5. django 中下载文件与下载保存为excel

    一.django 中下载文件 在实际的项目中很多时候需要用到下载功能,如导excel.pdf或者文件下载,当然你可以使用web服务自己搭建可以用于下载的资源服务器,如nginx,这里我们主要介绍dja ...

  6. SpringMVC实现从磁盘中下载文件

    除了文件的上传我们还需要从磁盘下载 实现文件的下载只要编写一个控制器,完成读写操作和响应头和数据类型的设置就可以了 下面演示的是从G盘imgs文件夹中下载文件 具体代码如下 package com.c ...

  7. java 从网络Url中下载文件 (转)

    http://blog.csdn.net/xb12369/article/details/40543649/ /**       * 从网络Url中下载文件       * @param urlStr ...

  8. 使用curl在命令行中下载文件

    http://m.blog.csdn.net/blog/mayadong7349/7019208 使用curl在命令行中下载文件 linux下curl简单应用详解 http://blog.sina.c ...

  9. java 从网络Url中下载文件

    转自:http://blog.csdn.net/xb12369/article/details/40543649 /** * 从网络Url中下载文件 * @param urlStr * @param ...

随机推荐

  1. 如何从Win7中提取制作Windows PE3.0

    在D盘新建文件夹winpe,在winpe中新建sources.pe3和new文件夹,把附件中提供的工具imagex连文件夹一起放到winpe目录中. 制作方法: 1.把windows7光盘(或光盘镜像 ...

  2. Spring注解@Component、@Repository、@Service、@Controller,@Autowired、@Resource用法

    一.Spring定义bean,@Component.@Repository.@Service 和 @Controller Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥 ...

  3. Loadrunner执行Java脚本

    1. Eclipse中调试Java脚本,脚本调试通过后,打成jar包: 2. 新建lr脚本,选择Java vuser协议: 3. Run-time Settings中Classpath设置jar包,没 ...

  4. 在Python中的格式化

    str= '@SES/%i/'%-1print strstr1='@SES/%i/'%1print str1str2='@SES/%i/'%2print str2 打印出的结果: @SES/-1/@S ...

  5. 【转】Kinect使用

    文章转自http://blog.csdn.net/yangtrees/article/details/16106271 Kinect中深度值最大为4096mm (0x0fff) 微软建议在开发中使用1 ...

  6. linux c遍历文件夹 和文件查找的方法

    linux c遍历文件夹的方法比较简单,使用c来实现 #include <iostream> #include <stdio.h> #include <sys/types ...

  7. mmap的使用

    http://blog.csdn.net/adcxf/archive/2009/03/14/3989725.aspx 共 享内存可以说是最有用的进程间通信方式,也是最快的IPC形式.两个不同进程A.B ...

  8. Android SharedPreference 数据存储

    参考:http://www.cnblogs.com/friends-wf/p/4835818.html 应用开发过程中,数据存储几乎是肯定会遇到的问题,根据要存储的数据类型和数量,可以选择合适的存储方 ...

  9. SignalR Supported Platforms -摘自网络

    SignalR is supported under a variety of server and client configurations. In addition, each transpor ...

  10. PC-红警联机问题与下载

    或许不是软件问题: 你做好相关设置了吗? 红警局域网联机的具体方法: 适用于原版红警.尤里复仇,及任何同样的扩展版. 第一步:安装IPX协议. 方法: 控制面板——网络连接(或网上邻居·属性)——本地 ...