asp.net下载文件的
常用方法(转)asp2009-08-3121:50:05阅读196评论0字号:大中小
//TransmitFile实现下载
//TransmitFile实现下载
protectedvoidButton1_Click1(objectsender,EventArgse)
{
/*
微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite
下载超过400mb的文件时导致
Aspnet_wp.exe进程回收而无法成功下载的
问题。
代码如下:
*/
stringstrFileName="三部闲置设备
管理系统操作手册IEMS.ppt";
Response.ContentType="application/x-zip-compressed";
//Response.ContentEncoding=System.Text.Encoding.GetEncoding("gb2312");
stringfilename=BLL.Config.PART_EM_UPLOAD_DOC+strFileName;
//BLL.Config.PART_EM_UPLOAD_DOC为路径("D:/EMUpl
oadDoc/")
Response.AddHeader("Content-Disposition","attachment;filename="+Server.UrlPathEncode(strFileName));
//Server.UrlPathEncode()解决文件名的乱码问题.
Response.TransmitFile(filename);
}//WriteFile实现下载
protectedvoidButton2_Click(objectsender,EventArgse)
{
/*
usingSystem.IO;
*/
stringfileName="asd.txt";//客户端保存的文件名
stringfilePath=Server.MapPath("DownLoad/aaa.txt");//路径
FileInfofileInfo=newFileInfo(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();