.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 2396 Budget (上下界网络流有源可行流)
转载: http://blog.csdn.net/axuan_k/article/details/47297395 题目描述: 现在要针对多赛区竞赛制定一个预算,该预算是一个行代表不同种类支出.列代表 ...
- cf 1263
A #include<bits/stdc++.h> using namespace std; int main(){ int t;cin>>t; while(t--){ ]; ...
- windows系统的安装时间怎么查看
方法一:利用命令符窗口查询 直接按下Windows+R组合键 出现运行对话框(或 点击开始—运行),输入cmd,进入命令符窗口 然后,在该界面下输入”systeminfo”,然后回车,等待系统自动运 ...
- 31 July
P1005 矩阵取数游戏 高精度不能更坑-- #include <cstdio> #include <cstring> struct INT { long long h, l; ...
- sql查询语句得到返回值fetchone()
需求: 现在mysql中有一张表,表名是info,我想通过报案号4201820330114401021497在这张表里查询出它对应的id. sql = "select claim_id fr ...
- 自定义实现字符串string的接口
用char*管理String类的内存,new动态分配,在析构函数中delete char*指向的new出来的内存,一个string类需要实现那些接口可参考标准库里的string: http://ww ...
- CSS 6种完全居中最佳实践(整理)
2016年4月28日 1.最佳法: .Absolute-Center { width: 50%; height: 50%; overflow: auto; margin: auto; position ...
- python基本数据类型集合set操作
转:https://www.cnblogs.com/tina-python/p/5468495.html 一.集合的定义 set集合,是一个无序且不重复的元素集合. 集合对象是一组无序排列的可哈希的值 ...
- mongo数据库基本查询语句
D:\MongoDB\Server\3.4\bin>mongo MongoDB shell version v3.-g83c3022fe4 connecting to: mongodb://12 ...
- C++中的转换构造函数
1,类型转换函数主要功能就是做类型转换,类型转换是将一个数据从 A 类型转换 到 B 类型,有隐式类型转换和强制类型转换两种: 2,再论类型转换: 1,标准数据类型之间会进行隐式的类型安全转换: 1 ...