.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. 两个结构体ifconf和ifreq

    用ioctl获得本地ip地址时要用到两个结构体ifconf和ifreq,它们对于大多数人来说都是比较陌生的,这里给大家一种比较简单的理解方法,当然只一种帮助理解的方法,在描述中可能会有一些地方与真实定 ...

  2. ADO.NET——获取output 和 return值

    程序代码 //存储过程 //Create PROCEDURE MYSQL // @a int, // @b int //AS // return @a + @b //GO SqlConnection ...

  3. [King.yue]EXT.Grid行双击事件

    .Listeners(l => {l.CellDblClick.Handler = string.Format(@"alert('xx');"); })

  4. 关于 Unity 中 ModelImporter.optimizeGameObjects

         ModelImporter.optimizeGameObjects 能够优化骨骼动画,将无用的骨骼合并,效率测试官方给出的数据差距比较大( Unity CJ 干货分享:全新的Unity移动游 ...

  5. platform机制

    最近在看SPI.I2C这样简单点的总线驱动程 序,从Linux2.6起,内核引入了一套新的驱动管理和注册机制:Platform_device和Platform_driver.现在Linux中 大部分的 ...

  6. c++面试常见160问

    1.在C++ 程序中调用被C 编译器编译后的函数,为什么要加extern "C"?答:首先,extern是C/C++语言中表明函数和全局变量作用范围的关键字,该关键字告诉编译器,其 ...

  7. Spring笔记之(一)初探

    对spring框架的学习我是从模拟它的简单实现开始,这样也易于领悟到它的整个框架结构,以下是简单实现的代码: 配置文件:spring.xml <?xml version="1.0&qu ...

  8. poj 2942--Knights of the Round Table (点的双连通分量)

    做这题简直是一种折磨... 有n个骑士,骑士之间相互憎恨.给出骑士的相互憎恨的关系. 骑士要去开会,围成一圈坐,相互憎恨的骑士不能相邻.开会骑士的个数不能小于三个人.求有多少个骑士不能开会. 注意:会 ...

  9. string,vector和array(C++ Primer读书笔记)

    string string是标准库类型,使用时需要包涵头文件,使用using声明. include <string> using std::string; 1.定义和初始化 string ...

  10. 校友信息管理系统&SNS互动平台之用户需求及概要设计

    前言.提纲及说明: 请移步:<校友信息管理&SNS互动平台之前言.目录及说明>(博客园地址:http://www.cnblogs.com/s6cn/p/3516876.html) ...