首页 新闻 搜索 专区 学院

文件下载问题

0
悬赏园豆:5 解决于 2012-06-12 13:27
 string fname = "系统操作手册.doc";
            
            string fullPath = HttpContext.Current.Request.PhysicalApplicationPath + "Asset\\SysHelp\\" + PublicMethod.AppSetting("AssetVesion") + "\\" + fname;

            bool blRet = true;

            if (File.Exists(fullPath))
            {
                try
                {
                    Response.AddHeader("Accept-Ranges", "bytes");
                    Response.Buffer = false;
                    FileStream activeXfile = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    BinaryReader br = new BinaryReader(activeXfile);

                    long fileLen = activeXfile.Length;
                    long startBytes = 0;

                    double pack = 10240;
                    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", (fileLen - startBytes).ToString());
                    Response.ContentType = "application/octet-stream";
                    Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fname, System.Text.Encoding.UTF8));

                    br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
                    int maxCount = (int)Math.Floor((fileLen - startBytes) / pack) + 1;

                    for (int i = 0; i < maxCount; i++)
                    {
                        if (Response.IsClientConnected)
                        {
                            Response.BinaryWrite(br.ReadBytes(int.Parse(pack.ToString())));
                            Thread.Sleep(sleep);
                        }
                        else
                        {
                            i = maxCount;
                        }
                    }
                }
                catch
                {
                    blRet = false;
                }
            }
            else
            {
                blRet = false;
            }

            return blRet;

这样下载文件,在VS里调试可以下载,部署到IIS上就不能下载

文件权限已经给了everyone,完全控制 

菜鸟吃虫,虫吃菜的主页 菜鸟吃虫,虫吃菜 | 初学一级 | 园豆:34
提问于:2012-06-12 11:27
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册