//方法一:TransmitFile实现下载
string fileName = "ss.docx";                          //客户端预设的文件名,导出时可修改 
string filePath = Server.MapPath("~/AssessReport/评估报表.docx");          //目标文件路径
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.TransmitFile(filePath);
注释:微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题 
       //方法二:WriteFile实现下载
string fileName = "ss.docx";                               //客户端保存的文件名 
       string filePath = Server.MapPath("~/AssessReport/评估报表.docx");          //目标文件路径
       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分块下载
string fileName = "ss.docx";                               //客户端保存的文件名 
       string filePath = Server.MapPath("~/AssessReport/评估报表.docx");
       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=" + 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();
      }
 
        //方法四:流方式下载
  string fileName = "aaa.docx";                            //客户端保存的文件名 
        string filePath = Server.MapPath("~/AssessReport/评估报表.docx");
        //以字符流的形式下载文件 
        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();


 

C#从服务器下载文件的四种方式的更多相关文章

  1. asp.net 浏览器下载文件的四种方式

    // 方法一:TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) { Response.ContentT ...

  2. C++服务器下载文件的两种方式

    #include <afxinet.h>#include "wininet.h" #pragma comment( lib, "wininet.lib&quo ...

  3. linux服务器之间传输文件的四种方式

    linux文件传输在内网渗透中至关重要,所以我在此总结一下几种Linux服务器之间传输文件的四种方式 1. scp [优点]简单方便,安全可靠:支持限速参数[缺点]不支持排除目录[用法]scp就是se ...

  4. java读取XML文件的四种方式

    java读取XML文件的四种方式 Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT& ...

  5. 从后端接口下载文件的2种方式:get方式、post方式

    从后端接口下载文件的2种方式 一.get方式 直接使用: location.href='http://www.xxx.com/getFile?params1=xxx&params2=xxxx' ...

  6. 【文件下载】Java下载文件的几种方式

    [文件下载]Java下载文件的几种方式  摘自:https://www.cnblogs.com/sunny3096/p/8204291.html 1.以流的方式下载. public HttpServl ...

  7. linux创建文件的四种方式(其实是两种,强行4种)

    linux创建文件的四种方式: 1.vi newfilename->i->编辑文件->ESC->:wq! 2.touch newfilename 3.cp sourcePath ...

  8. 解析xml文件的四种方式

    什么是 XML? XML 指可扩展标记语言(EXtensible Markup Language) XML 是一种标记语言,很类似 HTML XML 的设计宗旨是传输数据,而非显示数据 XML 标签没 ...

  9. spring引用hibernate映射文件的四种方式

    1.mappingResources 2.mappingLocations 3.mappingDirectoryLocations 4.mappingJarLocations 四种方式:https:/ ...

随机推荐

  1. 第11组 Alpha冲刺(4/6)

    第11组 Alpha冲刺(4/6)   队名 不知道叫什么团队 组长博客 https://www.cnblogs.com/xxylac/p/11884529.html 作业博客 https://edu ...

  2. arcgis python pdf合并

    # -*- coding: cp936 -*- import arcpy, os, string #Read input parameters from script tool PDFList = s ...

  3. Centos - php5.4升级到7.1 yum安装

    查看当前 PHP 版本 1 php -v 查看当前 PHP 相关的安装包,删除之 1 2 3 4 5 yum list installed | grep php   yum remove php   ...

  4. C#中正则表达式解析字符串信息

    正则表达式提取0~9数字 private static string RegexPickupNumber(string str) { string pattern = @"[^0-9]+&q ...

  5. kotlin之操作符重载

    一元操作符 表达式 对应的函数 +a a.unaryPlus() -a a.unaryMinus() !a a.not() a++ a.inc() a-- a.dec() fun main(arg: ...

  6. Python3中_和__的用途和区别

    访问可见性问题 对于上面的代码,有C++.Java.C#等编程经验的程序员可能会问,我们给Student对象绑定的name和age属性到底具有怎样的访问权限(也称为可见性).因为在很多面向对象编程语言 ...

  7. 010-HTTP协议

    一.概述 HTTP协议(HyperText Transfer Protocol,超文本传输协议)是用于从WWW服务器传输超文本到本地浏览器的传输协议.它可以使浏览器更加高效,使网络传输减少.它不仅保证 ...

  8. nodejs连接mongodb(密码)

    const MongoClient = require('mongodb').MongoClient; const url = 'mongodb://user:password@localhost:2 ...

  9. [转]德哥的PostgreSQL私房菜 - 史上最屌PG资料合集

    链接地址:https://yq.aliyun.com/articles/59251

  10. 【ARTS】01_22_左耳听风-201900408~2019004014

    ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...