FineUI 官方论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

本论坛已关闭(禁止注册、发帖和回复)
请移步 三石和他的朋友们

FineUI首页 WebForms - MVC & Core - JavaScript 常见问题 - QQ群 - 十周年征文活动

FineUI(开源版) 下载源代码 - 下载空项目 - 获取ExtJS - 文档 在线示例 - 版本更新 - 捐赠作者 - 教程

升级到 ASP.NET Core 3.1,快、快、快! 全新ASP.NET Core,比WebForms还简单! 欢迎加入【三石和他的朋友们】(基础版下载)

搜索
查看: 3000|回复: 1
打印 上一主题 下一主题

appbpx框架下下载文件的代码不起作用

[复制链接]
回帖奖励 4 金钱 回复本帖可获得 1 金钱奖励! 每人限 1 次
跳转到指定楼层
楼主
发表于 2012-10-1 11:15:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
如题,哪位能指点一下,谢谢!具体代码如下:
public static bool DownloadFile( string sUrl, string clientFileName)
        {
            
            string filePath = HttpContext.Current.Server.MapPath(sUrl);//路径         
            FileInfo fileInfo = new FileInfo(filePath);
            FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            BinaryReader binReader = new BinaryReader(fileStream);
            try
            {
               
                if (fileInfo.Exists)
                {
                    long startBytes = 0;
                    string lastUpdateTiemStamp = File.GetLastWriteTimeUtc(filePath).ToString("r");
                    string _EncodedData = HttpUtility.UrlEncode(clientFileName, Encoding.UTF8) + lastUpdateTiemStamp;
                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.Buffer = false;
                    HttpContext.Current.Response.AddHeader("Accept-Ranges", "bytes");
                    HttpContext.Current.Response.AppendHeader("ETag", "\"" + _EncodedData + "\"");
                    HttpContext.Current.Response.AppendHeader("Last-Modified", lastUpdateTiemStamp);
                    HttpContext.Current.Response.ContentType = "application/octet-stream";
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name);
                    HttpContext.Current.Response.AddHeader("Content-Length", (fileInfo.Length - startBytes).ToString());
                    HttpContext.Current.Response.AddHeader("Connection", "Keep-Alive");
                    HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
                    //Send data
                    binReader.BaseStream.Seek(startBytes, SeekOrigin.Begin);
                    //Dividing the data in 1024 bytes package
                    int maxCount = (int)Math.Ceiling((fileInfo.Length - startBytes + 0.0) / 1024);
                    //Download in block of 1024 bytes
                    int i;
                    for (i = 0; i < maxCount && HttpContext.Current.Response.IsClientConnected; i++)
                    {
                        HttpContext.Current.Response.BinaryWrite(binReader.ReadBytes(1024));
                        HttpContext.Current.Response.Flush();
                    }
                    //if blocks transfered not equals total number of blocks
                    if (i < maxCount)
                        return false;
                    return true;
                }
                else
                    return false;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                return false;
            }
            finally
            {
                HttpContext.Current.Response.End();
                binReader.Close();
                fileStream.Close();
            }
        
        }
沙发
发表于 2012-10-4 05:50:52 | 只看该作者

回帖奖励 +1 金钱

在IIS调试和在VS直接调试,下载的路径是不一样的
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|FineUI 官方论坛 ( 皖ICP备2021006167号-1 )

GMT+8, 2024-4-28 02:06 , Processed in 0.046307 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表