0

Google Chrome has this really neat feature that it opens .pdf Files in a new tab if supplied by the page correctly on download.

I was wondering how to achieve this in the latest revision of .NET Core?

My current configuration looks like this:

Backend:

[HttpGet("download")]
public IActionResult DownloadHelpFile()
{
      string path = Path.Combine(_environment.WebRootPath, "files/dummy.pdf");
      var net = new System.Net.WebClient();
      var data = net.DownloadData(path);
      var content = new MemoryStream(data);
      var contentType = "application/pdf";
      var fileName = "dummy.pdf";
      return File(content, contentType, fileName);
}

Frontend:

<a asp-action="DownloadHelpFile" _target="blank">@CasesLocalizer["NeedHelpDownloadOurGuide"]</a> &nbsp;&nbsp;

I also swapped _target= 'blank' with 'new'. I have searched on google and on StackOverflow and did not find anything updated for ASP.NET Core that works.

It still just downloads the file regularly but does not display the PDF in a new tab. What is my code missing?

beggarboy
  • 173
  • 1
  • 12
  • What happens if you remove the _target attribute? Does that open the PDF in the current tab like you want? – mason Mar 03 '21 at 17:51
  • Try to use FileResult instead of IActionResult as return type – NAS Mar 03 '21 at 19:03
  • 1
    https://stackoverflow.com/questions/1510451/how-to-return-pdf-to-browser-in-mvc – NAS Mar 03 '21 at 19:13
  • @mason I had no _target attribute before, that would just download the file regularly like any other file. – beggarboy Mar 04 '21 at 11:17
  • @NAS thanks a lot, you guided me towards the right source for this. Replacing `return File()` with `return FileStreamResult()` actually brings up the PDF Handler in Chrome, FireFox, Edge. One Problem remains, although it's superficial: The File does not open in a new tab. I tried both _target="blank" and "new". It just overrides the current tab with the PDF file. – beggarboy Mar 04 '21 at 11:59
  • 1
    @beggarboy I think it should be target="_blank" not _target="blank – NAS Mar 04 '21 at 13:41
  • @NAS Oh damn, you're absolutely correct. Everything works perfect now. Would you like to answer the thread with updated code snippets so future users have it easier? – beggarboy Mar 04 '21 at 15:48
  • great to hear, yes, I would like to answer – NAS Mar 04 '21 at 15:50
  • @beggarboy I answer the question – NAS Mar 04 '21 at 16:06

1 Answers1

2

You need to return FileStreamResult

[HttpGet("download")]
public IActionResult DownloadHelpFile()
{
      string path = Path.Combine(_environment.WebRootPath, "files/dummy.pdf");
      var net = new System.Net.WebClient();
      var data = net.DownloadData(path);
      var content = new MemoryStream(data);
      var contentType = "application/pdf";
      var fileName = "dummy.pdf";
      return FileStreamResult (content, contentType, fileName);
}

and use target="_blank" instead of _target="blank

<a asp-action="DownloadHelpFile" target="_blank">@CasesLocalizer["NeedHelpDownloadOurGuide"]</a> &nbsp;&nbsp;

NAS
  • 311
  • 2
  • 6