惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Hackread – Cybersecurity News, Data Breaches, AI and More
宝玉的分享
宝玉的分享
月光博客
月光博客
爱范儿
爱范儿
阮一峰的网络日志
阮一峰的网络日志
酷 壳 – CoolShell
酷 壳 – CoolShell
Recent Announcements
Recent Announcements
A
About on SuperTechFans
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
U
Unit 42
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
Martin Fowler
Martin Fowler
N
Netflix TechBlog - Medium
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
V
Visual Studio Blog
腾讯CDC
IT之家
IT之家

博客园 - 风也无奈

事件学习 信息系统需求分析阶段的实践经验之二---如何有效地获得用户需求【转】 信息系统需求分析阶段的实践经验之一---需求分析概述[转] Lambda表达式【转】 C#委托的介绍(delegate、Action、Func、predicate)【转】 “DllRegisterServer的调用失败”问题解决办法 什么是RFID技术 SQL Server集群服务器的优缺点 asp中的ckEditor的详细配置 UTF8转成GB2312乱码问题解决思路 winform更新程序代码 C#开发串口总结,并提炼串口辅助类到公用类库中 SQLXML]FOR XML语法导出XML的易错之处 C#通用类库--QQ吸附窗体类 C#通用类库--数字转为人民币汉字大写表示 - 风也无奈 - 博客园 link和@import的区别 - 风也无奈 - 博客园 C#操作word并格式化 Winform中的DataGridView控件内容自动保存《转载》 SQL中获取一个长字符串中某个字符串出现次数的简单方法
C#下载功能代码<转>
风也无奈 · 2011-01-07 · via 博客园 - 风也无奈

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {

 }

 //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();

 }
}