一、//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 = ;//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 > && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, , Convert.ToInt32(ChunkSize));//读取的大小
Response.OutputStream.Write(buffer, , 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, , 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 = ;
  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 = ;
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(); }

C# 从服务器下载文件代码的几种方法的更多相关文章

  1. 【FTP】C# System.Net.FtpClient库连接ftp服务器(下载文件)

    如果自己单枪匹马写一个连接ftp服务器代码那是相当恐怖的(socket通信),有一个评价较高的dll库可以供我们使用. 那就是System.Net.FtpClient,链接地址:https://net ...

  2. Java 从服务器下载文件到本地(页面、后台、配置都有)

    先来看实现效果: 有一个链接如下: 点击链接下载文件:  第一种方法:Servlet实现 一.HTML页面部分: 1.HTML页面中的一个链接 <a id="downloadTempl ...

  3. Java Web实现使用浏览器从服务器下载文件(后台)

    Java Web实现 使用浏览器从服务器下载文件. 下面实现两种情况的下载,需求如下: 需求(一):1.用户在页面填写表单. 2.填写完成后,选择下载,将表单内容发往后台. 3.后台根据内容生产一个文 ...

  4. (4)FTP服务器下载文件

    上一篇中,我们提到了怎么从FTP服务器下载文件.现在来具体讲述一下. 首先是路径配置.. 所以此处我们需要一个app.config来设置路径. <?xml version="1.0&q ...

  5. Python 实现批量从不同的Linux服务器下载文件

    基于Python实现批量从不同的Linux服务器下载文件   by:授客 QQ:1033553122 实现功能 1 测试环境 1 使用方法 1 1. 编辑配置文件conf/file_for_downl ...

  6. 从Linux服务器下载文件到本地命令

    从Linux服务器下载文件夹到本地1.使用scp命令 scp /home/work/source.txt work@192.168.0.10:/home/work/ #把本地的source.txt文件 ...

  7. 从Linux服务器下载文件夹到本地

    从Linux服务器下载文件夹到本地 1.使用scp命令 scp /home/work/source.txt work@192.168.0.10:/home/work/ #把本地的source.txt文 ...

  8. YbSoftwareFactory 代码生成插件【二十五】:Razor视图中以全局方式调用后台方法输出页面代码的三种方法

    上一篇介绍了 MVC中实现动态自定义路由 的实现,本篇将介绍Razor视图中以全局方式调用后台方法输出页面代码的三种方法. 框架最新的升级实现了一个页面部件功能,其实就是通过后台方法查询数据库内容,把 ...

  9. 安卓手机下载YouTube视频的3种方法

    作为全球最大的在线视频网站,YouTube上面的内容可真是应有尽有啊,从教学视频到个人手工艺品制作流程,从各种搞笑视频到电视连续集等等,包罗万象.如果你想下载YouTube视频到电脑上面的话,网上有很 ...

随机推荐

  1. 忘记redhat linux root密码怎么办

    自己VM虚拟机里安装redhat linux root密码忘了,刚开始想重新安装,但是后来想到还有linux有一个single模式,使用single可以直接进入字符界面,然后修改: 启动vm虚拟机,启 ...

  2. Linux 内核Coding Style整理

    转载:http://www.cnblogs.com/wang_yb/p/3532349.html 总结linux内核开发的coding style, 便于以后写代码时参考. 下面只是罗列一些规则, 具 ...

  3. BootStrap2学习日记8---表单

    <form> <label for="username">用户名</label> <input id="username&quo ...

  4. Android进阶笔记15:ListView篇之图片优化

    1.图片异步加载: (1)处理图片的方式: 如果ListView中自定义的Item中有涉及到大量图片的,一定要对图片进行细心的处理,因为图片占的内存是 ListView 项中最头疼的,处理图片的方法大 ...

  5. mysql颠覆实战笔记(六)--商品系统设计(三):商品属性设计之固定属性

    今天我们来讲一下商品属性 我们知道,不同类别的商品属性是不同的. 我们先建一个表prod_class_attr:

  6. Ubuntu 14.04 LTS 与Kylin

    现在是安装了Ubuntu 14.04 LTS 但是通过安装ubuntukylin 这个包居然实现了Kylin--原来这个自主研发还这么方便-呵呵 sudo apt-get install ubuntu ...

  7. jfreechart的使用案例

    一:下载需要的2个jar文件 jcommon-版本号.jar,jfreechart-版本号.jar,注意版本不要太高. 实例一:比较简单的application版本的饼图 /** * */ packa ...

  8. mysql 修改 character_set_database 编码格式

    操作系统:win10  x64 Server version : 5.5.46 MySQL Community Server (GPL) mysql 修改 character_set_database ...

  9. Java HashCode方法

    有许多人学了很长时间的Java,但一直不明白hashCode方法的作用,  我来解释一下吧.首先,想要明白hashCode的作用,你必须要先知道Java中的集合.   总的来说,Java中的集合(Co ...

  10. Evaluation of Expression Tree

    Evaluation of Expression Tree Given a simple expression tree, consisting of basic binary operators i ...