0

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)
SSD
  • 1,041
  • 3
  • 19
  • 39
  • Part of me thinks you're fighting a browser setting. It looks like there are ways to trick the browser into a file save dialog instead by modifying the header content-disposition. See this thread for some ideas: https://stackoverflow.com/questions/12035410/save-download-file-dialog – kd345205 Jun 22 '21 at 18:15
  • @kd345205 I don't want to use file dialog. just download the file from server in the same window without opening. – SSD Jun 22 '21 at 18:39

1 Answers1

1

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");
    }
Sekhar
  • 5,614
  • 9
  • 38
  • 44