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

推荐订阅源

F
Full Disclosure
WordPress大学
WordPress大学
小众软件
小众软件
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
腾讯CDC
量子位
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
Scott Helme
Scott Helme
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
Jina AI
Jina AI
Attack and Defense Labs
Attack and Defense Labs
S
SegmentFault 最新的问题
Simon Willison's Weblog
Simon Willison's Weblog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
Google Online Security Blog
Google Online Security Blog
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
罗磊的独立博客
L
LINUX DO - 最新话题
博客园 - Franky
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
AI
AI
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
Help Net Security
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
I
Intezer
S
Securelist

博客园 - 微雨杏花村

CentOS firewalld 防火墙操作 CentOS 安装 dotnetcore CentOS7防火墙设置--iptables CentOS7.0 安装 Nginx Win10下Hyper-V设置网络连接 亲测Google开源JPEG图片压缩算法,可将JPEG文件缩小%35 android 术语 一些比较好用的网站整站下载工具 ASP.NET Page Life Cycle 获取网页URL地址及参数等的两种方法(js和C#) SQL跨数据库读取数据的方法 .net 注册和删除Windows服务 IIS7 下部署 MVC的注意事项 生成网站页面的截图 windows 下andriod 开发环境的搭建 无法上网,无线网络总是受限制或无连接 bcp命令详解-转载 searcherinder.exe .net 生成静态页
下载的四种方法
微雨杏花村 · 2011-04-13 · via 博客园 - 微雨杏花村

View Code

//下载的四种方法
protected void Button1_Click(object sender, EventArgs e)
        {
            
//TransmitFile实现下载

            
/**/
            
/*
             微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite
             下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。
             代码如下:
            
*/

            Response.ContentType 

= "application/x-zip-compressed";
            Response.AddHeader(
"Content-Disposition""attachment;filename=keji.rar");
            
string filename = Server.MapPath("keji.rar");
            Response.TransmitFile(filename);
            Response.Write(
"<script language=\"javascript\" type=\"text/javascript\">");
            Response.Write(
"alert(\"下载成功\");");
            Response.Write(
"window.location.href=\"C_SC.aspx\";");
            Response.Write(
"</script>");
        }
protected void Button2_Click(object sender, EventArgs e)
        {
//WriteFile实现下载
            string fileName = "ceshi.rar";//客户端保存的文件名
            string filePath = Server.MapPath("keji.rar");//路径

            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();
        }
protected void Button3_Click(object sender, EventArgs e)
        {
            
//WriteFile分块下载

            
string fileName = "GhostXP.iso";//客户端保存的文件名
            string filePath = Server.MapPath("GhostXP7.7.iso");//路径

            System.IO.FileInfo fileInfo 
= new System.IO.FileInfo(filePath);if (fileInfo.Exists == true)
            {
                
const long ChunkSize = 409600;//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 = "ce2.rar";//客户端保存的文件名
            string filePath = Server.MapPath("keji.rar");//路径//以字符流的形式下载文件
            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();
        }