1

让我说:

点击第一个链接弹出另存为框,然后点击取消。下一步,

点击第二个链接弹出另存为框,然后点击取消。下一步,

点击第三个链接弹出另存为框,然后点击取消。</p>

此刻,弹出框不显示,页面一样忙!

win7 中的情况。XP下没问题。

对不起,我的英语不好!索引.aspx:

<a href="ShowFile.aspx?fileID=1" >download</a>
<a href="ShowFile.aspx?fileID=2" >download</a>
<a href="ShowFile.aspx?fileID=3">download</a>
<a href="ShowFile.aspx?fileID=4" >download</a>

ShowFile.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
        {

            Page.Response.Clear();
            bool success = ResponseFile(Page.Request, Page.Response, "1.doc", @"d:\1.doc", 1024000);
            if (!success)
                Response.Write("dowload error!");
            Page.Response.End();
        }
        public static bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed)
        {
            try
            {
                FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                BinaryReader br = new BinaryReader(myFile);
                try
                {
                    _Response.AddHeader("Accept-Ranges", "bytes");
                    _Response.Buffer = false;
                    long fileLength = myFile.Length;
                    long startBytes = 0;

                    double pack = 10240; //10K bytes
                    //int sleep = 200;   //
                    int sleep = (int)Math.Floor(1000 * pack / _speed) + 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(int.Parse(pack.ToString())));
                            Thread.Sleep(sleep);
                        }
                        else
                        {
                            i = maxCount;
                        }
                    }
                }
                catch
                {
                    return false;
                }
                finally
                {
                    br.Close();

                    myFile.Close();
                }
            }
            catch
            {
                return false;
            }
            return true;
        }
4

1 回答 1

1

通常,当您创建一个用于下载文件的页面时,您最好使用处理程序,或者至少在其 aspx 页面禁用会话时。

原因是会话会锁定用户直到完成,并且当您下载文件时通常需要很长时间,或者如果您停止它可以堆栈直到了解网络已关闭。

因此,禁用此 aspx 页面的会话可以解决您的问题。

相对:
在共享同一会话时处理另一个 Web 应用程序时 Web 应用程序被阻止
哪些性能计数器可用于识别 ASP.NET 瓶颈?
完全替换 ASP.Net 的会话
尝试使 Web 方法异步

于 2013-07-03T08:10:30.930 回答