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

推荐订阅源

cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
博客园_首页
GbyAI
GbyAI
罗磊的独立博客
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
人人都是产品经理
人人都是产品经理
U
Unit 42
V
Visual Studio Blog
F
Fortinet All Blogs
小众软件
小众软件
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
LangChain Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Engineering at Meta
Engineering at Meta
aimingoo的专栏
aimingoo的专栏
The Cloudflare Blog
T
Tor Project blog
Martin Fowler
Martin Fowler
K
Kaspersky official blog
Scott Helme
Scott Helme
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
D
DataBreaches.Net
博客园 - Franky
阮一峰的网络日志
阮一峰的网络日志
博客园 - 【当耐特】
P
Proofpoint News Feed
N
Netflix TechBlog - Medium
美团技术团队
S
Secure Thoughts
C
Cisco Blogs
M
MIT News - Artificial intelligence
L
Lohrmann on Cybersecurity
T
Tenable Blog
N
News and Events Feed by Topic
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Check Point Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Spread Privacy
Spread Privacy
S
Security @ Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
A
Arctic Wolf
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Hacker News: Front Page
T
Threat Research - Cisco Blogs
Simon Willison's Weblog
Simon Willison's Weblog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
O
OpenAI News
V
Vulnerabilities – Threatpost

博客园 - 风也无奈

事件学习 信息系统需求分析阶段的实践经验之二---如何有效地获得用户需求【转】 信息系统需求分析阶段的实践经验之一---需求分析概述[转] 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();

 }
}