Javascript 例外情况:;远程主机关闭了连接。错误代码为0x80070057“;

Javascript 例外情况:;远程主机关闭了连接。错误代码为0x80070057“;,javascript,c#,asp.net-mvc,Javascript,C#,Asp.net Mvc,我得到了这个例外。 我遵循了报告中给出的建议 不过,我还是犯了同样的错误 我正在使用Response.WriteFile()将文件从服务器传输到客户端浏览器 鉴于: $("#btnExport").on("click", function (e) { window.location = '@Url.Action("ExportToExcel", "Report")'; e.preventDefault(); }); 在控制器中: [HttpGet] publ

我得到了这个例外。 我遵循了报告中给出的建议

不过,我还是犯了同样的错误

我正在使用
Response.WriteFile()
将文件从服务器传输到客户端浏览器

鉴于:

  $("#btnExport").on("click", function (e) {
      window.location = '@Url.Action("ExportToExcel", "Report")';
       e.preventDefault();
  });
在控制器中:

[HttpGet]
public RedirectResult ExportToExcel()
{
    Download(ExportFilePath);    
    return new RedirectResult(ExportFilePath); 
}

public void Download(ExportFilePath)
{
    HttpContext context = System.Web.HttpContext.Current;
    FileInfo file = new FileInfo(ExportFilePath);
    context.Response.Clear();
    context.Response.ClearHeaders();
    context.Response.ClearContent();
    context.Response.AppendHeader("Content-Disposition", "attachment; filename =" + ExportFileName);
    context.Response.AppendHeader("Content-Length", file.Length.ToString());
    context.Response.ContentType = "application/excel";
    context.Response.WriteFile(file.FullName);
    context.Response.Flush();
    context.Response.Close();
    context.Response.End();
}

我也面临同样的问题,您应该尝试删除下面的代码行

context.Response.WriteFile(file.FullName);
context.Response.Flush();
context.Response.Close();
context.Response.End();
添加波纹管行

context.Response.TransmitFile(strFileName);
解决方案2:

FileStream myFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
long FileSize = myFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
myFileStream.Read(Buffer, 0, (int)FileSize);
myFileStream.Close();
myFileStream.Dispose();

Response.ContentType = "image/jpeg";
Response.AddHeader("Content-Type", "image/jpeg");
Response.AddHeader("Content-Disposition", "attachment;filename=FILENAME.jpg");

Response.BinaryWrite(Buffer);
Response.End();

如果上述解决方案不适合您,请告诉我。

您的
重定向结果(ExportFilePath)
有点奇怪。它应该是url,页面应该重定向到url,而不是文件path@rock_walker:我可以将Download()方法放在什么地方?建议的操作实现是什么。@BhaweshPaliwal该错误仅仅意味着用户在将所有内容从服务器发送到客户端之前关闭了浏览器,例如客户端请求文件,服务器开始传输文件,但在服务器完成发送文件之前,用户关闭浏览器。。。。基本上你对此无能为力。@BhaweshPaliwal。我使用简单的*.txt文件在本地主机上应用了您的代码示例。没有错误,我成功下载了文件。@BhaweshPaliwal。如果您使用VS2013和/或IIS8,则可能是信号器问题。尝试在VS中禁用BrowserLink。对不起,我只能猜到这个建议,因为VS2012和IISExpress安装在我的主机上。谢谢!您的两种解决方案对我都非常有效。然后,请接受这一回答并投票+1:)