Search code examples
javascriptc#asp.net-mvcmemorystream

ASP.NET MVC - Download PDF file in same window not working


I have download PDF file(not to open) generated from controller method. file is getting opened in new separate window. I am generating MemoryStream at server side. I have to return it to client in same window, here I don't have to open, just download in the same client window. below code I have tried -

Server-

public async Task<ActionResult> DownloadReport(string id, string reportType="")
{
  var fileData = await GetReport(id, reportType); 
  // here fileData is MemoryStream

  return  File(fileData, "application/pdf");
}

html code -

 @Html.ActionLink("Download", "DownloadReport","Files", new { id = "abc" },null)

Solution

  • Use Content-Disposition header

       public async Task<ActionResult> DownloadReport(string id, string reportType="")
        {
          var fileData = await GetReport(id, reportType); 
          // here fileData is MemoryStream
          Response.AddHeader("Content-Disposition", "attachment;filename=file.pdf");
          return  File(fileData, "application/pdf");
        }