在大约ASP.Net下载文件到客户端浏览器的win7一个错误? [英] A bug in win7 about ASP.Net Download file to client browser?

查看:113
本文介绍了在大约ASP.Net下载文件到客户端浏览器的win7一个错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说:

点击第一个链接弹出另存为对话框,然后单击cancel.Next,

点击第二个链接弹出另存为对话框,然后单击cancel.Next,

点击第三个链接弹出另存为对话框,然后单击取消。

目前,弹出对话框不显示和网页一样忙着!

只是在Win7条件。在XP中没有任何问题。

对不起我的英语不好!
Index.aspx的:

 < A HREF =ShowFile.aspx FILEID = 1?>下载和LT; / A>
&所述; A HREF =ShowFile.aspx FILEID = 2?>下载及下; / A>
&所述; A HREF =ShowFile.aspx FILEID = 3?>下载及下; / A>
< A HREF =ShowFile.aspx FILEID = 4?>下载和LT; / A>

ShowFile.aspx.cs:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
        {            Page.Response.Clear();
            BOOL成功= ResponseFile(Page.Request,Page.Response,1.DOC,@D:\\ 1.DOC,1024000);
            如果(!成功)
                的Response.Write(下载中心的错误!);
            Page.Response.End();
        }
        公共静态布尔ResponseFile(HTT prequest _REQUEST,Htt的presponse _response,串_filename,串_fullPath,长_SPEED)
        {
            尝试
            {
                的FileStream MYFILE =新的FileStream(_fullPath,FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
                BinaryReader BR =新BinaryReader(MYFILE);
                尝试
                {
                    _Response.AddHeader(接受范围,字节);
                    _Response.Buffer = FALSE;
                    长文件长度= myFile.Length;
                    长startBytes = 0;                    双包= 10240; // 10K字节
                    // INT睡眠= 200; //
                    INT睡眠=(int)的Math.Floor(1000 *包/ _SPEED)+ 1;
                    如果(_Request.Headers [范围]!= NULL)
                    {
                        _Response.Status code = 206;
                        。字符串[] =范围_Request.Headers [范围]斯普利特(新的char [] {'=',' - '});
                        startBytes = Convert.ToInt64(范围[1]);
                    }
                    _Response.AddHeader(内容长度,(文件长度 - startBytes)的ToString());
                    如果(startBytes!= 0)
                    {
                        //Response.AddHeader(\"Content-Range的String.Format(字节{0} - {1} / {2},startBytes,文件长度-1,文件长度));
                    }
                    _Response.AddHeader(连接,保持活动);
                    _Response.ContentType =应用程序/八位字节流;
                    _Response.AddHeader(内容处置,附件;文件名=+ HttpUtility.UrlEn code(_filename,System.Text.Encoding.UTF8));                    br.BaseStream.Seek(startBytes,SeekOrigin.Begin);
                    INT MAXCOUNT =(int)的Math.Floor((文件长度 - startBytes)/包)+ 1;                    的for(int i = 0; I< MAXCOUNT;我++)
                    {
                        如果(_Response.IsClientConnected)
                        {
                            _Response.BinaryWrite(br.ReadBytes(int.Parse(pack.ToString())));
                            Thread.sleep代码(睡眠);
                        }
                        其他
                        {
                            I = MAXCOUNT;
                        }
                    }
                }
                抓住
                {
                    返回false;
                }
                最后
                {
                    br.Close();                    myFile.Close();
                }
            }
            抓住
            {
                返回false;
            }
            返回true;
        }


解决方案

通常,当你犯了一个页面,用于下载的文件你更好的使用处理,或至少如果aspx页面关闭会话。

究其原因是因为会话是锁定用户,直到完成是,当你下载一个文件,你通常需要较长时间,或如果你停止它可以堆叠,直到明白网络是封闭的。

所以禁用会话这个aspx页面能够解决您的问题。

相关:结果
<一href=\"http://stackoverflow.com/questions/9426673/web-app-blocked-while-processing-another-web-app-on-sharing-same-session\">Web应用程序被阻止在处理上共享同一个会话结果另一个Web应用程序
<一href=\"http://stackoverflow.com/questions/9056240/what-perfmon-counters-are-useful-for-identifying-asp-net-bottlenecks/9056650#9056650\">What性能计数器对于识别ASP.NET瓶颈有用吗?结果
完全替换ASP.Net的会话结果
<一href=\"http://stackoverflow.com/questions/11325150/trying-to-make-web-method-asynchronous/11325285#11325285\">Trying使Web方法异步

Let me say:

Click the first link pop up the save as box,then click cancel。Next,

Click the second link pop up the save as box,then click cancel。Next,

Click the third link pop up the save as box,then click cancel。

At the moment,The pop up box do not show and the page same as busying!

The condition just in win7 . In XP no problem.

Sorry for my bad english! Index.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;
        }

解决方案

Usually when you make a page for downloading a file you better use a handler, or at least if its aspx page disable the session.

The reason is because the session is lock the user until is finish, and when you download a file you usually take long time, or if you stop it can be stack until understand that the network is closed.

So disabling the session for this aspx page can solve your issue.

relative:
Web app blocked while processing another web app on sharing same session
What perfmon counters are useful for identifying ASP.NET bottlenecks?
Replacing ASP.Net's session entirely
Trying to make Web Method Asynchronous

这篇关于在大约ASP.Net下载文件到客户端浏览器的win7一个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆