我得到了这个例外。 我遵循了给出的建议 What does this error mean? The remote host closed the connection. The error code is 0x80070057
仍然,我收到同样的错误。
我正在使用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();
}
答案 0 :(得分:6)
我遇到了同样的问题,你应该尝试删除下面的代码行
context.Response.WriteFile(file.FullName);
context.Response.Flush();
context.Response.Close();
context.Response.End();
添加Bellow line
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();
如果以上解决方案对您不起作用,请告诉我。