日期:2010-01-20  浏览次数:20504 次

复制代码 代码如下:

public partial class FileDownLoad : System.Web.UI.Page
{
//提供下载的文件,不编码的话文件名会乱码
private string fileName = HttpContext.Current.Server.UrlEncode("规范.rar");
private string filePath = HttpContext.Current.Server.MapPath("规范.rar");
//使用TransmifFile下载文件
protected void btnDL1_Click(object sender, EventArgs e)
{
FileInfo info = new FileInfo(filePath);
long fileSize = info.Length;
Response.Clear();
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename="+ fileName);
//不指明Content-Length用Flush的话不会显示下载进度
Response.AddHeader("Content-Length", fileSize.ToString());
Response.TransmitFile(filePath, 0, fileSize);
Response.Flush();
Response.Close();
}

//使用WriteFile下载文件
protected void btnDL2_Click(object sender, EventArgs e)
{
FileInfo info = new FileInfo(filePath);
long fileSize = info.Length;
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName);
//指定文件大小
Response.AddHeader("Content-Length", fileSize.ToString());
Response.WriteFile(filePath, 0, fileSize);
Response.Flush();
Response.Close();
}

//使用OutputStream.Write分块下载文件
protected void btnDL3_Click(object sender, EventArgs e)
{
//指定块大小
long chunkSize = 102400;
//建立一个100K的缓冲区
byte[] buffer = new byte[chunkSize];
//已读的字节数
long dataToRead = 0;
FileStream stream = null;
try
{
//打开文件
stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
dataToRead = stream.Length;

//添加Http头
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName);
Response.AddHeader("Content-Length", dataToRead.ToString());

while (dataToRead > 0)
{
if (Response.IsClientConnected)
{
int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize));
Response.OutputStream.Write(buffer, 0, length);
Response.Flush();
Response.Clear();
dataToRead -= length;
}
else
{
//防止client失去连接
dataToRead = -1;
}
}
}
catch (Exception ex)
{
Response.Write("Error:" + ex.Message);
}
finally
{
if (stream != null)
{
stream.Close();
}
Response.Close();
}
}

//使用BinaryWrite下载文件,大文件效率不行
protected void btnDL4_Click(object sender, EventArgs e)
{
FileStream stream = null;
try
{
//读文件,大文件一次读入会占用大量内存
stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
stream.Close();

//添加Http头
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName);
Response.AddHeader("Content-Length", bytes.Length.ToString());
Response.BinaryWrite(bytes);
Response.Flush();
}
catch (Exception ex)
{
Response.Write("Error:" + ex.Message);
}
finally
{
if (stream != null)
{
stream.Close();
}
Response.Close();
}
}
//使用BinaryWrite分块下载文件
protected void btnDL5_Click(object sender, EventArgs e)
{
//指定区块和缓冲区
long chunkSize = 102400;
byte[] buffer = new byte[chunkSize];
FileStream stream = null;
long dataToRead = 0;
try
{
stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
dataToRead = stream.Length;
//添加Http头
Response.ContentType = "application/octet-stream";

相关资料更多>