惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
MyScale Blog
MyScale Blog
博客园 - 【当耐特】
I
InfoQ
腾讯CDC
aimingoo的专栏
aimingoo的专栏
L
LangChain Blog
人人都是产品经理
人人都是产品经理
D
DataBreaches.Net
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
Google DeepMind News
Google DeepMind News
Vercel News
Vercel News
C
Check Point Blog
B
Blog RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
Y
Y Combinator Blog
D
Docker
MongoDB | Blog
MongoDB | Blog
量子位
博客园_首页

博客园 - Angelo Dell'inferno

[ASP.NET] 基于.Net Remoting 的网站访问监控模块 [转]Run a Http Response Filter together with an Ajax Update Panel [ASP.NET] HyperLink + Image 实现动态图片链接 [ASP.NET] (转)地址栏参数的判断 [ASP.NET] 实现Label自动换行 [ASP.NET] 验证码生成 [ASP.NET] 实现客户端浏览服务端目录的页面 [ASP.NET]Treeview 控件显示服务端目录文件夹及文件 [C#] GridView导出到Excel [C#] 获取两个时间点的时间间隔 [SQL Server][转]数据库并发控制——活锁&死锁 [JavaScript] 防止页面被嵌入Iframe [SQL Server] 存储过程事务 [SQL Server] T-SQL 连接数据库方法 [C#] 杀Excel进程 [ASP.NET] (原创)自定义GridView分页 [C#] 将DataSet内容导入到Excel (矩阵区域导出) [Oracle] 日期相关操作 [ORACLE] 函数大全
[ASP.NET] 服务器端下载文件实现
Angelo Dell'inferno · 2007-12-10 · via 博客园 - Angelo Dell'inferno

            string _fileName = HttpContext.Current.Server.MapPath("~\\report\\FactReports\\") + e.Item.Cells[0].Text.Trim(); //服务器端文件路径
            //建立文件对象
            FileStream myFile = new FileStream(_fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            //建立流读对象
            BinaryReader br = new BinaryReader(myFile);
            Response.AddHeader("Accept-Ranges", "bytes");
            Response.Buffer = false;
            long fileLength = myFile.Length;
            long startBytes = 0;

            int pack = 10240; //10K bytes
            //int sleep = 200;   //每秒5次   即5*10K bytes每秒
            int sleep = (int)Math.Floor(1000 * pack / 1024000) + 1;
            if (Request.Headers["Range"] != null)
            {
                Response.StatusCode = 206;
                string[] range = Request.Headers["Range"].Split(new char[] {'=', '-'});
                startBytes = Convert.ToInt64(range[1]);
            }
            Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
            if (startBytes != 0)
            {
                Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength));
            }
            Response.AddHeader("Connection", "Keep-Alive");
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(_fileName,System.Text.Encoding.UTF8) );

            br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
            int maxCount = (int) Math.Floor((fileLength - startBytes) / pack) + 1;
            for (int i = 0; i < maxCount; i++)
            {
                if (Response.IsClientConnected)
                {
                    Response.BinaryWrite(br.ReadBytes(pack));
                    Thread.Sleep(sleep);
                }
                else
                {
                    i=maxCount;

                }
            }
            myFile.Close();
            br.Close();
            Response.End();