asp.netfancybox

Fancybox not displaying PDF but will download it


I am using FancyBox to display PDF files. When accessing the file directly through the file system, it works perfectly.

However, due to file restrictions we have, I am unable to do this through the file system directly. I have created an endpoint that returns a FileResult through ASP.NET API but as a result, the FancyBox is downloading the file instead of showing it. The return File object has a content type of System.Net.Mime.MediaTypeNames.Application.Pdf

I'm not sure of the best approach to handle this and the documentation is sparse.

The link looks like

<a role="button" data-document-id="@(document.id)" data-fancybox href="~/api/DownloadDocument?documentID=@document.Id&type=@(document.extension)" class="btn btn-success">Download</a>

Solution

  • Rather than returning a FileResult object, return the file in a stream (FileStreamResult). This will produce the same behaviour as referencing the file path directly and allow it to be rendered in FancyBox as opposed to immediately downloading.