.net文件下载的四种方法
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.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)
{
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();
}
}
.net文件下载的四种方法的更多相关文章
- Fedora安装qt总结四种方法
在fedora上安装qt有四种方法,本人由于初次接触fedora,所以还是耐心的把三个方法都测试了一遍. 1. 下载源码,手动编译,选择路径安装,请参考<fedora15下搭建QT开发环境及编 ...
- 两个变量交换的四种方法(Java)
对于两种变量的交换,我发现四种方法,下面我用Java来演示一下. 1.利用第三个变量交换数值,简单的方法. (代码演示一下) class TestEV //创建一个类 { public static ...
- 织梦DedeCMS模板防盗的四种方法
织梦(DedeCMS)模板也是一种财富,不想自己辛辛苦苦做的模板被盗用,在互联网上出现一些和自己一模一样的网站,就需要做好模板防盗.本文是No牛收集整理自网络,不过网上的版本都没有提供 Nginx 3 ...
- 让一个图片在div中居中(四种方法)
第一种方法: <div class="title"> <div class="flag"></div> <div cl ...
- 运行jar应用程序引用其他jar包的四种方法
转载地址:http://www.iteye.com/topic/332580 大家都知道一个java应用项目可以打包成一个jar,当然你必须指定一个拥有main函数的main class作为你这个ja ...
- java中定时器的四种方法
package com.lid; import java.util.Calendar; import java.util.Date; import java.util.Timer; import ja ...
- Angular--页面间切换及传值的四种方法
1. 基于ui-router的页面跳转传参(1) 在AngularJS的app.js中用ui-router定义路由,比如现在有两个页面,一个页面(producers.html)放置了多个produce ...
- MYSQL获取自增ID的四种方法
MYSQL获取自增ID的四种方法 1. select max(id) from tablename 2.SELECT LAST_INSERT_ID() 函数 LAST_INSERT_ID 是与tabl ...
- linux下配置ip地址四种方法(图文方法)
主要是用第四种方法 (1)Ifconfig命令 第一种使用ifconfig命令配置网卡的ip地址.此命令通常用来零时的测试用,计算机启动后 ip地址的配置将自动失效.具体用法如下.Ipconfig ...
随机推荐
- POJ 3130 How I Mathematician Wonder What You Are! (半平面相交)
Description After counting so many stars in the sky in his childhood, Isaac, now an astronomer and a ...
- 【Linux】grep显示匹配行的上下几行的用法
打印匹配行的前后5行 grep -5 ‘something’ file 打印匹配行的前后5行 grep -C 5 ‘something’ file 打印匹配行的后5行 grep -A 5 ‘somet ...
- 九、async、future、packaged_task、promise
std::async.std::future创建后台任务并返回值. 希望线程返回一个值. std::async是个函数模板,用来启动一个异步任务,返回一个std::future对象 异步任务:自动创建 ...
- 【HDOJ6598】Harmonious Army(最小割)
题意:有n个人,每个人可以从A,B两种职业中选择一种 有m对两人组,如果两个人都是A能获得p的收益,一个A一个B能获得q的收益,都是B能获得r的收益,其中q=p/4+r/3,保证p%4=0,r%3=0 ...
- AcWing 260. 买票 (树状数组+二分)打卡
题目:https://www.acwing.com/problem/content/description/262/ 题意:给定一个队伍,每个人过来的时候可以插队,每个人会输入一个插入到哪个位置,但是 ...
- php7和MongoDB插入并读取数据
php7和MongoDB插入并读取数据 代码如下: <?php $manager = new MongoDB\Driver\Manager("mongodb://localhost:2 ...
- python类与对象练习题扑克牌
#定义一个扑克类,属性是颜色,数字.#定义一个手类,属性是扑克牌得颜色数字#定义一个人类,属性是左手,右手.类里定义一些方法,比如交换,展示 class Poker : def __init__(se ...
- jquery 合并两个 json 对象
jQuery.extend( [ deep ], target, object1, [ objectN ] )合并对象到第一个对象 //deep为boolean类型,其它参数为object类型 var ...
- 测开之路四十一:常用的jquery函数
jQuery选择器菜鸟教程:https://www.runoob.com/jquery/jquery-selectors.html 引用jquery2.1.1标签:<script src=&qu ...
- 读取 appsettings.json
Appsettings.json 配置: 个配置文件就是一个json文件,并且是严格的json文件,所有的属性都需要添加“”引号.下图是一个常规的代码示例: {"UrlString" ...