asp.net中文件下载功能的实例代码
时间:2023-07-20 15:46:51
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();
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python类的继承链实例分析
盘点Python 爬虫中的常见加密算法
![](https://img.aspxhome.com/file/2023/5/124245_0s.jpg)
JS操作Cookies的小例子
Django获取该数据的上一条和下一条方法
单点登录之cas集成sonar的配置方法
![](https://img.aspxhome.com/file/2023/0/132320_0s.jpg)
oracle快速删除重复的记录
如何获取机器的网络配置属性?
pandas 实现分组后取第N行
![](https://img.aspxhome.com/file/2023/5/75295_0s.jpg)
Python+微信接口实现运维报警
MySql数据库时间序列间隔查询方式
利用python程序生成word和PDF文档的方法
Flask教程之重定向与错误处理实例分析
详细解读Python中的__init__()方法
mac下重置mysl8.0.11密码的方法
什么是好的设计
深入理解Pytorch中的torch. matmul()
![](https://img.aspxhome.com/file/2023/5/107175_0s.jpg)
用Python编写分析Python程序性能的工具的教程
![](https://img.aspxhome.com/file/2023/3/63553_0s.jpg)
SQL Server 数据库操作实用技巧锦集
浅析Banner构成与创意设计
![](https://img.aspxhome.com/file/UploadPic/200911/28/originality-design-55s.jpg)