2013-07-03 35 views
1

让我说:有关ASP.Net的win7中的错误将文件下载到客户端浏览器?

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

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

单击第三连杆弹出另存为对话框,然后单击取消。

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

条件只是在win7中。在XP中没有问题。

对不起,我的英语不好! 的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; 
     } 
+0

其更好地使用下载文件的处理程序,反正在ASPX禁用该页面的会话,然后再试一次。 – Aristos

+1

@ Aristos-感谢您的帮助,您是对的!禁用会话。工作就像一个魅力。你是我的明星 – user1662936

+0

好吧,然后我给它回答 – Aristos

回答

1

通常当你做一个网页下载文件您更好的使用处理,或至少如果aspx页面禁用会话。

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

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

相对:
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

+0

@ user1662936不客气。 – Aristos

相关问题