.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();

}

}

时间: 2024-08-05 02:26:30

.net文件下载的四种方法的相关文章

Fedora安装qt总结四种方法

在fedora上安装qt有四种方法,本人由于初次接触fedora,所以还是耐心的把三个方法都测试了一遍. 1.  下载源码,手动编译,选择路径安装,请参考<fedora15下搭建QT开发环境及编译QT>,博主亲测通过. 优点:       可以自主选择需要的qt版本可以自主选择安装路径 缺点: 编译耗时,其次需要手动安装很多依赖库. 2.  通过yum命令安装,请参考<yum安装qt>,这个其实和第四种方法本质是一样的,一个命令式,一个通过界面而已. sudo yum instal

转载:CSS实现三栏布局的四种方法示例

转载网址:http://www.jb51.net/css/529846.html 前言 其实不管是三栏布局还是两栏布局都是我们在平时项目里经常使用的,也许你不知道什么事三栏布局什么是两栏布局但实际已经在用,或许你知道三栏布局的一种或两种方法,但实际操作中也只会依赖那某一种方法,本文具体的介绍了三栏布局的四种方法,并介绍了它的使用场景. 所谓三栏布局就是指页面分为左中右三部分然后对中间一部分做自适应的一种布局方式. 1.绝对定位法 HTML代码如下: <div class="left&quo

两个变量交换的四种方法

对于两种变量的交换,我发现四种方法,下面我用Java来演示一下. 1.利用第三个变量交换数值,简单的方法. class TestEV //创建一个类 { public static void main(String[]args) { int x =5,y=10; //定义两个变量 int temp = x; //定义第三临时变量temp并提取x值 x = y; //把y的值赋给x y = temp; //然后把临时变量temp值赋给y System.out.println("x="+x

java中定时器的四种方法

1 package com.lid; 2 3 import java.util.Calendar; 4 import java.util.Date; 5 import java.util.Timer; 6 import java.util.TimerTask; 7 8 public class Test { 9 public static void main(String[] args) { 10 //timer1(); 11 timer2(); 12 //timer3(); 13 //time

自学Linux命令的四种方法

自学Linux命令的四种方法 导读 童鞋们刚接触linux时,在学习过程中中会遇到不少问题,学习linux摸不着头脑,那么下面介绍四种linux的学习方法,特别适合新手. 方法一:终端"每日提示" 在.bashrc中(/home/.bashrc)增加如下一行: echo "Did you know that:"; whatis$(ls /bin | shuf -n 1) 你只需要增加这行就够了!如果你想让它更娱乐化一些,你可以安装cowsay.Ubuntu/Debi

【转】Java中字符串中子串的查找共有四种方法(indexof())

原文网址:http://wfly2004.blog.163.com/blog/static/1176427201032692927349/ Java中字符串中子串的查找共有四种方法,如下:1.int indexOf(String str) :返回第一次出现的指定子字符串在此字符串中的索引. 2.int indexOf(String str, int startIndex):从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引. 3.int lastIndexOf(String st

IOS中Json解析的四种方法

作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSON格式化校验(点击打开链接).此网站不仅可以检测Json代码中的错误,而且可以以视图形式显示json中的数据内容,很是方便. 从IOS5开始,APPLE提供了对json的原生支持(NSJSONSerialization),但是为了兼容以前的ios版本,可以使用第三方库来解析Json. 本文将介绍TouchJso

Android提交数据到服务器的两种方式四种方法

Android应用开发中,会经常要提交数据到服务器和从服务器得到数据,本文主要是给出了利用http协议采用HttpClient方式向服务器提交数据的方法. /** * @author Dylan * 本类封装了Android中向web服务器提交数据的两种方式四种方法 */ public class SubmitDataByHttpClientAndOrdinaryWay { /** * 使用get请求以普通方式提交数据 * @param map 传递进来的数据,以map的形式进行了封装 * @p

AVA实例化类的四种方法

原文地址:JAVA实例化类的四种方法 作者:权镜士 JAVA中实例化类的四种方法 1)使用new操作符 2)调用Class对象的newInstance()方法 3)调用clone()方法,对现有实例的拷贝 4)通过ObjectInputStream的readObject()方法反序列化类 点击(此处)折叠或打开 import java.io.*; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTa