2016-11-15 162 views
0

我已经继承了一个旧网站,该网站具有下载用户记录的Excel文档的功能。下面的代码是造成“远程主机关闭了连接错误代码是0x800704CD。”错误:“远程主机关闭连接”错误HttpResponse.Write

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Web; 

namespace MySite.ApplicationServices 
{ 
public class OutputFileWriter : IDisposable 
{ 
    private HttpResponse _response; 
    private bool _isResponsePrepared; 

    public OutputFileWriter(HttpResponse response) 
    { 
     this._response = response; 
    } 

    public OutputFileWriter(HttpResponse response, string outputFileName) 
     : this(response) 
    { 
     this.OutputFileName = outputFileName; 
    } 

    public string OutputFileName { get; set; } 

    public virtual void WriteLine(string line) 
    { 
     if (this._response == null) 
      throw new ObjectDisposedException("OutputFileWriter"); 

     if (!this._isResponsePrepared) 
     { 
      this.PrepareResponse(); 
      this._isResponsePrepared = true; 
     } 

     this._response.Write(line); 
    } 

    public virtual void Dispose() 
    { 
     if (this._response != null) 
     { 
      this._response.Flush(); 
      this._response.Close(); 
      this._response = null; 
     } 
    } 

    protected virtual void PrepareResponse() 
    { 
     if (string.IsNullOrEmpty(this.OutputFileName)) 
      throw new InvalidOperationException("An output file name is required."); 

     this._response.Clear(); 
     this._response.ContentType = "application/octet-stream"; 
     this._response.Buffer = this._response.BufferOutput = false; 
     this._response.AppendHeader("Cache-Control", "no-store, no-cache"); 
     this._response.AppendHeader("Expires", "-1"); 
     this._response.AppendHeader("Content-disposition", "attachment; filename=" + this.OutputFileName); 
    } 
} 
} 

下面是调用它(在点击“下载”按钮)的代码示例:

using (OutputFileWriter writer = new OutputFileWriter(this.Response, "users.xls")) 
      { 
       foreach (string result in searchResults) 
       { 
        writer.WriteLine(result); 
       } 
      } 

即使在下载了几个字节后也会发生错误。我知道,如果客户取消下载,在正常情况下可能会发生错误,但是当人们不取消时也会发生错误。我猜想连接正在丢失,但我不知道为什么。

万一它是相关的,该网站在IIS 7中配置集成应用程序池在.NET 2.0下。它也是负载平衡的。

有什么想法?

+0

可能想给[这个答案正确使用IDisposable接口](http://stackoverflow.com/a/538238/215552)阅读。我并不是说你做错了,但是当你处理'IDisposable'时,仔细检查一下是个好主意。 –

+0

@MikeMcCaughan奇怪的是,它似乎很好地在Visual Studio中本地运行。也许可能指向IIS或应用程序池设置。 – user806982

回答

0

经过一番进一步调查,似乎该文件实际上只能在Firefox中下载。

以下帖子建议从_response.Close()更改为_response.End()可能工作https://productforums.google.com/forum/#!topic/chrome/8Aykgxa8kWU,它的确如此。

不过,我后来看到这个HttpResponse.End vs HttpResponse.Close vs HttpResponse.SuppressContent

所以我现在在一个HttpApplication实例调用CompleteRequest()。就像这样:

老方法

public virtual void Dispose() 
{ 
    if (this._response != null) 
    { 
     this._response.Flush(); 
     this._response.Close(); 
     this._response = null; 
    } 
} 

的新方法:

public virtual void Dispose() 
{ 
    if (this._response != null) 
    { 
     this._response.Flush(); 
     this._application.CompleteRequest(); 
     this._response = null; 
    } 
} 

现在这是工作的罚款。

相关问题