博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 文件下载四方法
阅读量:6717 次
发布时间:2019-06-25

本文共 2833 字,大约阅读时间需要 9 分钟。

hot3.png

using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.IO;public partial class _Default : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){}//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/z.zip");Response.TransmitFile(filename);}//WriteFile实现下载protected void Button2_Click(object sender, EventArgs e){string fileName ="asd.txt";//客户端保存的文件名string filePath=Server.MapPath("DownLoad/aaa.txt");//路径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.txt";//客户端保存的文件名string filePath = Server.MapPath("DownLoad/aaa.txt");//路径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.txt";//客户端保存的文件名string filePath = Server.MapPath("DownLoad/aaa.txt");//路径//以字符流的形式下载文件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();}}

转自:

转载于:https://my.oschina.net/u/212572/blog/795965

你可能感兴趣的文章
How to Limit NodeRunner.exe High Memory, CPU Usage
查看>>
solr7.1.0学习笔记(10)---Solr发布到Tomcat
查看>>
洛谷P1435 回文字串(dp)
查看>>
php 会话控制(关于session的维护与生命周期)
查看>>
tomcat PermGen space
查看>>
高阶函数:声明、实现(定义)与调用
查看>>
splash 安装
查看>>
mysql数据库优化课程---15、mysql优化步骤
查看>>
数据库路由中间件MyCat - 使用篇(4)
查看>>
Java程序开发中的简单内存分析
查看>>
Java中的Future相关
查看>>
CGAL Catmull-Clark Subdivide Surface
查看>>
赛车入门 -- 专有技术名词
查看>>
接收IWebBrowser2的自动化事件
查看>>
需求入门: 需求工程=需求开发+需求管理
查看>>
androidmanifest.xml权限中文说明
查看>>
matlab练习程序(感知哈希对比图片)
查看>>
多媒体指令(图像灰度化)
查看>>
sqlserver数据库大型应用解决方案总结
查看>>
枚举系统设备
查看>>