/*
* asp.net 几种文件下载方式比较
*
* 方法1、HttpResponse.TransmitFile
* 方法2、HttpResponse.WriteFile
* 方法3、HttpResponse.BinaryWrite
* 方法4、HttpResponse.Redirect
*
* 方法1与方法2
* 相同点:都是通过文件的相对或绝对路径下载文件;
* 不同点:方法2是一次性将文件读入内存,然后输出给客户端;
* 方法1不在内存中缓冲文件。
*
* 因此,对于大文件或用户数多的下载,方法1不会对于服务器内存的占用将远远低于方法2;
* 这正是方法1的最大优势,
* 但方法1也有一个局限:does not work with UNC-share file paths. *
* UNC (Universal Naming Convention) / 通用命名规则,也叫通用命名规范、通用命名约定。
* 它符合 \servername\sharename 格式
* 也就是说方法1无法下载网络共享磁盘的文件
*
* 例如:
* if (filePath.StartsWith(@"\\"))
* context.Response.WriteFile(filePath, false);
* else
* context.Response.TransmitFile(filePath);
*
* 方法3
* 方法3主要是将已有的btye[] 型对象输出到客户端;
* 如果要下载的文件位于数据库等存储介质,那么,读入内存时一般可放于DataTable等对象中,
* 这时就可以直接HttpResponse.BinaryWrite((byte[])dt.Rows[0]["fileContent"])输出
*
* 方法4
* 方法4主要是通过文件的相对路径下载文件; *
*
*
* 以上四个方法,如果下载一个汉字命名且字数超过20个字的文件
* 方法1不会有问题;
* 使用其它三个方法下载后,如果客户端在提示框中点“打开”将报错,提示文件名过长。
*
*/ 以下是测试用到的 download.aspx 及 download.cs的代码:
download.aspx
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="download.aspx.cs" Inherits="download" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div> <asp:Button ID="btnDownloadByRedirect" runat="server"
Text="using Response.Redirect()" onclick="btnDownloadByRedirect_Click"
/>
<asp:Button ID="btnDownloadByTransmitFile" runat="server" Text="using Response.TransmitFile()"
onclick="btnDownloadByTransmitFile_Click" />
<asp:Button ID="btnDownloadByWriteFile" runat="server" Text="using Response.WriteFile()"
OnClick="btnDownloadByWriteFile_Click" />
<asp:Button ID="btnDownloadByBinaryWrite" runat="server" Text="using Response.BinaryWrite()"
OnClick="btnDownloadByBinaryWrite_Click" />
</div>
</form>
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
download.cs
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public partial class download : System.Web.UI.Page
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
protected void Page_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//System.Threading.Thread.Sleep(5000);
//Download(HttpContext.Current.Response);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected void btnDownloadByRedirect_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Download(HttpContext.Current.Response);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected void btnDownloadByTransmitFile_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Download(HttpContext.Current.Response, DownloadMethod.TransmitFile);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected void btnDownloadByWriteFile_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Download(HttpContext.Current.Response, DownloadMethod.WriteFile);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected void btnDownloadByBinaryWrite_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Download(HttpContext.Current.Response, DownloadMethod.BinaryWrite);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private FileInfo info = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private enum DownloadMethod
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WriteFile,
BinaryWrite,
TransmitFile
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private bool PrepareFile()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string applicationPath = HttpContext.Current.Request.PhysicalApplicationPath;
string lastCharacter = applicationPath.Substring(applicationPath.Length - 1);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!lastCharacter.Equals("\\"))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
applicationPath = applicationPath + "\\";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
info = new FileInfo(applicationPath + "Files\\测试文件测试文件测试文件测试文件测试文件测.xls");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return info.Exists;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private byte[] GetFileByte()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return File.ReadAllBytes(info.FullName);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private long GetFileLength()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return info.Length;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void Download(HttpResponse response)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (PrepareFile())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
response.Redirect("Files/" + info.Name, true);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void Download(HttpResponse response, DownloadMethod method)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (PrepareFile())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
response.Clear();
response.ClearHeaders();
response.Buffer = false;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string displayName = HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(info.Name));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
response.AppendHeader("Content-Disposition", "attachment;filename=" + displayName);
response.ContentType = "application/octet-stream";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
switch (method)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case DownloadMethod.WriteFile:
//response.WriteFile(info.FullName);
response.TransmitFile("Files/" + info.Name);
break;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case DownloadMethod.BinaryWrite:
response.BinaryWrite(GetFileByte());
break;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case DownloadMethod.TransmitFile:
//response.TransmitFile(info.FullName);
response.TransmitFile("Files/" + info.Name);
break;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
response.Flush();
response.End();
}
}