asp.net中文件下载功能的实例代码

时间:2023-07-20 15:46:51 


//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/aaa.zip");
Response.TransmitFile(filename);
}

//WriteFile实现下载
protected void Button2_Click(object sender, EventArgs e)
{


string fileName ="aaa.zip";//客户端保存的文件名
string filePath=Server.MapPath("DownLoad/aaa.zip");//路径

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.zip";//客户端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.zip");//路径

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.zip";//客户端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.zip");//路径

//以字符流的形式下载文件
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();

}

标签:.net,文件下载
0
投稿

猜你喜欢

  • python类的继承链实例分析

    2023-05-16 14:04:36
  • 盘点Python 爬虫中的常见加密算法

    2023-07-19 05:05:22
  • JS操作Cookies的小例子

    2024-06-11 22:17:13
  • Django获取该数据的上一条和下一条方法

    2022-12-07 13:36:35
  • 单点登录之cas集成sonar的配置方法

    2022-01-17 00:45:58
  • oracle快速删除重复的记录

    2010-07-23 13:03:00
  • 如何获取机器的网络配置属性?

    2009-11-23 20:44:00
  • pandas 实现分组后取第N行

    2023-02-09 11:11:40
  • Python+微信接口实现运维报警

    2022-06-12 23:40:30
  • MySql数据库时间序列间隔查询方式

    2024-01-27 15:23:03
  • 利用python程序生成word和PDF文档的方法

    2023-08-26 13:31:44
  • Flask教程之重定向与错误处理实例分析

    2021-02-04 08:00:49
  • 详细解读Python中的__init__()方法

    2023-03-25 17:10:27
  • mac下重置mysl8.0.11密码的方法

    2024-01-28 06:27:50
  • 什么是好的设计

    2010-02-25 12:22:00
  • 深入理解Pytorch中的torch. matmul()

    2023-06-03 05:29:18
  • 用Python编写分析Python程序性能的工具的教程

    2022-02-13 01:57:03
  • SQL Server 数据库操作实用技巧锦集

    2009-01-20 13:20:00
  • 浅析Banner构成与创意设计

    2009-11-28 16:25:00
  • 使用游标进行PHP SQLSRV查询的方法与注意事项

    2023-05-22 10:51:10
  • asp之家 网络编程 m.aspxhome.com