using System;
using System.Web;
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";//客í户§端?保£存?的?文?件t名?
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";//客í户§端?保£存?的?文?件t名?
string filePath = Server.MapPath("DownLoad/aaa.txt");//路·径? var fileInfo = new FileInfo(filePath); if (fileInfo.Exists == true)
{
const long chunkSize = 102400;//100K 每?次?读á取?文?件t,?只?读á取?100K,?这a样ù可é以?缓o解a服t务?器÷的?压1力|
byte[] buffer = new byte[chunkSize]; Response.Clear();
FileStream iStream = File.OpenRead(filePath);
long dataLengthToRead = iStream.Length;//获?取?下?载?的?文?件t总ü大ó小?
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";//客í户§端?保£存?的?文?件t名?
string filePath = Server.MapPath("DownLoad/aaa.txt");//路·径? //以?字?符?流÷的?形?式?下?载?文?件t
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";
//通¨知a浏ˉ览à器÷下?载?文?件t而?不?是?打ò开a
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End(); }
}

ASP.NET 文件下载的更多相关文章

  1. ASP.NET文件下载各种方式比较:对性能的影响、对大文件的支持、对断点续传和多线程下载的支持

    asp.net里提供了多种方式,从服务器端向客户端写文件流,实现客户端下载文件.这种技术在做防下载系统时比较有用处.主些技术主要有:WriteFile.TransmitFile和BinaryWrite ...

  2. 用ASP实现文件下载

    <% '**************************************************************'**使用方法:                        ...

  3. asp.net 文件下载(txt,rar,pdf,word,excel,ppt)

    aspx 文件下载说起来一点都不难,但是在做的过程中还是遇到了一些小小的问题,就是因为这些小小的问题,导致解决起来实在是太难了,其中一个就是Response.End();导致下载文件出现线程终止的情况 ...

  4. asp.net文件下载文件另存为

    这是一个困惑已久的问题…… 首先,用<a>标签的href打开浏览器能解读的文件(如txt,jpg,pdf等),会自动打开,无法做到弹出另存为的效果. 其次,网上搜索了各种JS解决办法,包括 ...

  5. Asp.Net 文件下载1——流下载(适用于大文件且防盗链)(转)

    使用流防盗链下载大文件 直接上 Asp.net 后置代码好了 using System; using System.Data; using System.Configuration; using Sy ...

  6. asp.net文件下载

    protected void btn_Down(object sender, EventArgs e) { string filePath = Server.MapPath("/TradeL ...

  7. asp.net 文件下载显示中文名称

    protected void Page_Load(object sender, EventArgs e)    {        string guid = Request.QueryString[& ...

  8. ASP.netMVC文件下载的几种方法

    第一种:最简单的超链接方法,标签的href直接指向目标文件地址,这样容易暴露地址造成盗链,这里就不说了 第二种:后台下载 在后台下载中又可以细分为几种下载方式 首先,在前台,我们需要一个标签 &quo ...

  9. ASP.net MVC 文件下载的几种方法(欢迎讨论)

    在ASP.net MVC 中有几种下载文件的方法 前提:要下载的文件必须是在服务器目录中的,至于不在web项目server目录中的文件下载我不知道,但是还挺想了解的. 第一种:最简单的超链接方法,&l ...

随机推荐

  1. 强大好用的"文本"编辑器

    1 editplugs 说明:EditPlus是一款由韩国 Sangil Kim (ES-Computing)出品的小巧但是功能强大的可处理文本.HTML和程序语言的Windows编辑器,你甚至可以通 ...

  2. Spring AOP简述

    使用面想对象(Object-Oriented Programming,OOP)包含一些弊端,当需要为多个不具有继承关系的对象引入公共行为时,例如日志,安全检测等.我们只有在每个对象中引入公共行为,这样 ...

  3. [.net 面向对象编程基础] (14) 重构

    [.net 面向对象编程基础] (14) 重构 通过面向对象三大特性:封装.继承.多态的学习,可以说我们已经掌握了面向对象的核心.接下来的学习就是如何让我们的代码更优雅.更高效.更易读.更易维护.当然 ...

  4. 解如下方程(java实现)

    n                              (m=1) f(m,n)=  m                              (n=1) f(m-1,n)+f(m,n-1) ...

  5. [每日电路图] 1、基于AT89C52单片机最小系统接口电路【转】

              come from:http://www.21ic.com/dianlu/basis/interface/2015-04-21/621607.htm AT89C52是美国Atmel ...

  6. [ACM_几何] Wall

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28417#problem/E 题目大意:依次给n个点围成的一个城堡,在周围建围墙,要求围墙 ...

  7. read links July-14

    1)   http://ruby-hacking-guide.github.io/intro.html It has one part to discuss “Technique to read so ...

  8. Linux C/C++的编译

    以前在Linux上面编译过C,但是没有编译过C++,今天用到了,就稍微学习了一下. 简单的介绍 linux 中最重要的编译工具是 GCC.GCC 是 GNU 的 C 和 C++ 编译器.实际上,GCC ...

  9. Android开发学习之路-Palette颜色提取工具类使用

    视频(要FQ):https://www.youtube.com/watch?v=5u0dtzXL3PQ Palette是一个在support-v7包中的一个颜色提取工具类,用法比较简单,而且是谷歌官方 ...

  10. rabbitmq消息队列——"发布订阅"

    三."发布订阅" 上一节的练习中我们创建了一个工作队列.队列中的每条消息都会被发送至一个工作进程.这节,我们将做些完全不同的事情--我们将发送单个消息发送至多个消费者.这种模式就是 ...