I am getting this exception . I followed the suggestions given in What does this error mean? The remote host closed the connection. The error code is 0x80070057
still , I am getting the same error.
I am transferring a file from server to Client browser using Response.WriteFile()
.
In View:
$("#btnExport").on("click", function (e) {
window.location = '@Url.Action("ExportToExcel", "Report")';
e.preventDefault();
});
In Controller:
[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();
}
I was facing the same issue, you should try removing bellow lines of code
context.Response.WriteFile(file.FullName);
context.Response.Flush();
context.Response.Close();
context.Response.End();
add Bellow line
context.Response.TransmitFile(strFileName);
Solution 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();
let me know if above solution doesn't work for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With