skip to Main Content

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/[email protected]&type=@(document.extension)" class="btn btn-success">Download</a>

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. It has nothing to do with Fancybox. It just creates iframe element and points source to your file. So, if you can simply embed your PDF in your HTML document using <iframe> element, then it will also work in Fancybox. Fancybox is not parsing and rendering your file, you can use https://mozilla.github.io/pdf.js/ for that.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search