.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 ...
随机推荐
- Python3解leetcode Factorial Trailing Zeroes
问题描述: Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 ...
- 【Vue】新版vue解决跨域问题
vue.config.js module.exports = { devServer: { proxy: { "/api": { target: "http://192. ...
- Python基础教程(019)--执行Python的方式,IPython
前言 了解IPython 内容 IPython 是一个Python的交互式shell,比默认的Python shell 好用的多 查看图片 在提示符下执行 目的 了解进入IPython 退出IPyth ...
- 攻防世界 | CAT
来自攻防世界官方WP | darkless师傅版本 题目描述 抓住那只猫 思路 打开页面,有个输入框输入域名,输入baidu.com进行测试 发现无任何回显,输入127.0.0.1进行测试. 发现已经 ...
- SQL各种JOIN
JOIN(= INNER JOIN):返回匹配的结果,没有匹配则没结果: LEFT JOIN(= LEFT OUTER JOIN):返回匹配的与左表的所有数据: RIGHT JOIN(= RIGHT ...
- (转)Docker入门——Dockerfile详解
转:https://www.cnblogs.com/sorex/p/6481407.html 基本示例 FROM MAINTAINER LABEL RUN ADD COPY CMD ENTRYPOIN ...
- 项目搭建(三):自定义DLL
说明:程序中有些自定义的控件类型在TestStack.White框架中没有涉及,需要引入自定义的DLL,通过鼠标点击事件处理 使用:将自定义的ClassLibrary2.dll拷贝到项目/bin/de ...
- Asp.Net Core 第02局:Program
总目录 前言 本文介绍Program,它包含程序的入口Main方法.从这里开始... 环境 1.Visual Studio 2017 2.Asp.Net Core 2.2 开局 第一手:Program ...
- 关于函数lower_bound()如何使用的问题
这个函数是c++ STL里自带的函数,应该需要引用头文件#include<iostream> 功能:在一个有序的序列中查找可以将value(一个变量)放在队列里面而不会引起序列长度变化,单 ...
- JS原型与原型链终极详解 (转载)
这篇文章需要认认真真仔仔细细的看才能看懂 一. 普通对象与函数对象 JavaScript 中,万物皆对象!但对象也是有区别的.分为普通对象和函数对象,Object ,Function 是JS自带的函 ...