Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception : "The remote host closed the connection. The error code is 0x80070057"

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();
}
like image 559
Bhawesh Paliwal Avatar asked Jan 30 '15 08:01

Bhawesh Paliwal


1 Answers

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.

like image 141
Mox Shah Avatar answered Oct 22 '22 02:10

Mox Shah