.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. LA_3026_Period_(kmp)

    描述 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  2. VS2010中的调试技巧

    作者: scottgu 这是我的博客中关于VS 2010和.NET 4发布系列的第二十六篇文章. 今天的博文将介绍Visual Studio中的一些实用调试技巧.这是受我朋友Scott Cate (他 ...

  3. udhcpc和udhcpd移植

    实现DHCP自动获取IP地址 前提:系统已经实现DNS(即使用ping www.baidu.com测试时能ping通). 1.  在内核中添加以下选项: Networking  ---> [*] ...

  4. HDU 5651 xiaoxin juju needs help 水题一发

    分析:求一下组合数 首先,如果不止一个字符出现的次数为奇数,则结果为0. 否则,我们把每个字符出现次数除2,也就是考虑一半的情况. 那么结果就是这个可重复集合的排列数了. fact(n)/fact(a ...

  5. NOIP2000 乘积最大

    题二  乘积最大              (22分)    问题描述      今年是国际数学联盟确定的“2000——世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏 ...

  6. Already tried 0 time(s); retry policy is RetryUpToMaximumCountWithFixedSleep解决方法

    14/03/26 23:10:04 INFO ipc.Client: Retrying connect to server: 0.0.0.0/0.0.0.0:10020. Already tried ...

  7. Add And Reset a Federation Server to a Federation Server Farm adfs ad

    Applies To: Active Directory Federation Services (AD FS) 2.0 After you install the Active Directory ...

  8. MariaDB-5.5.33a 编译安装

    交代一下内核的信息 [root@localhost soft]# uname -r 2.6.32-71.el6.x86_64 创建mariadb用户组 [root@localhost mariadb- ...

  9. java_list<String> string[]拼接json

    private String getJsonStr(List<String> jsonKeyList, String[] values){ String jsonStr = "{ ...

  10. 10个可以直接拿来用的JQuery代码片段

    jQuery里提供了许多创建交互式网站的方法,在开发Web项目时,开发人员应该好好利用jQuery代码,它们不仅能给网站带来各种动画.特效,还会提高网站的用户体验. 本文收集了10段非常实用的jQue ...