0

In the Episerver back office I have a content refrence that has a PDF file as its data.

On the front end (Razor view) I am using this code to output the PDF

<a href="@Url.ContentUrl(Model.PdfMedia)" class="btn btn-primary" tabindex="-1">@Model.CurrentBlock.DownloadPdfText</a>

When a user clicks this link, the PDF is opened in the browser, I want the browser to download the PDF and NOT open it in the browser.

One thing I noticed is that the URL genrated is /globalassets/PDF-File-Name........ but this URL is not the URL to the actual location of the PDF file, how can I get this URL? and how can I force the browser to download the PDF rather than open it?

Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157
Ayo Adesina
  • 2,231
  • 3
  • 37
  • 71
  • This is a .NET MVC question rather than Episerver. https://stackoverflow.com/questions/26873180/provide-static-file-download-at-server-in-web-application – Eric Herlitz Apr 24 '18 at 09:39
  • Not sure about that it is an MVC question, yes the view is being rendered by an MVC controller, but the URL the Episerver API returns at the moment is not the actuall URL to the media as in App_data/blobs/filename.pdf – Ayo Adesina Apr 24 '18 at 09:42
  • If the PDF is opened in the browser epi is doing what it is supposed to. – Eric Herlitz Apr 24 '18 at 09:42
  • Yes, that is the case, but I want it that link to like to the FULL url of my file, surely this can be done in episerver – Ayo Adesina Apr 24 '18 at 10:20
  • "how can I force the browser to download the PDF rather than open it?"...opening it _is_ downloading it, albeit to a temporary location. It can't get to the user's browser without being downloaded from the server. Then the user can choose what to do with it. You can provide the link to the file, it's up to the browser what it chooses to do with it. Some browsers can read PDF files, some can't. There are some tricks you can do by setting certain headers in the response which may indicate to the browser that it should just save it, although YMMV. You can google about that. – ADyson Apr 24 '18 at 11:02
  • "this URL is not the URL to the actual location of the PDF file"...how do you know? I'm going to guess since it's a content-management system, there probably isn't a real direct URL to the file, it's possibly served from a database or other offline file store, via some server-side script which takes the filename (or other ID), retrieves the file content from the store, and serves it up as a download. This is quite a common way to do downloads, as it allows for much easier implementation of things like security and authorisaiton. – ADyson Apr 24 '18 at 11:05
  • @ADyson Yes exactly... so I want to get the actual URL for the file pragmatically – Ayo Adesina Apr 24 '18 at 12:40
  • ...and did you read / understand anything I wrote? e.g. How do you know there even _is_ another URL? Do you know what it would be, or what pattern it would follow? If such a thing exists, the product you're using will likely have documented it somewhere – ADyson Apr 24 '18 at 12:42
  • @ADyson yes I did understand, and yes there is another URL the CMS puts the files in the App_data folder, but I can't find a way how to get to that URL – Ayo Adesina Apr 24 '18 at 12:47
  • The App_data folder is not ment to be accessible to end users. Its whole point is to store data which is internal to the application. Normally there is no route to it from the webserver. If the CMS put the files there it's because the intention was not to expose them via a direct download URL - presumably for reasons including the ones I listed above. Anyway, why do you care? The code works, the file gets opened/downloaded (as per browser settings), so what's the issue? You never said what your purpose was. – ADyson Apr 24 '18 at 12:48

2 Answers2

6

Super easy. Just append /download to the URL.

<a href="@Url.ContentUrl(Model.PdfMedia)/download" class="btn btn-primary" tabindex="-1">@Model.CurrentBlock.DownloadPdfText</a>

Why do you want the physical URL to the file? That shouldn't be necessary and in most cases they are stored in e.g. Azure Blob Storage.

Johan Petersson
  • 972
  • 4
  • 13
  • Wow... works... but how, why? I only wanted the URL to the file becuase I thought that might force it to download, I have never seen just adding /download before...is that a episerver specific thing? – Ayo Adesina Apr 24 '18 at 15:23
  • 3
    That is Episerver-specific. It will set correct http headers. You also might want to set the download attribute on the anchor, please see https://www.w3schools.com/TAGS/att_a_download.asp – Johan Petersson Apr 24 '18 at 17:17
  • Thanks a lot nice to know, never come across this before. - Well there is a first time for everything. – Ayo Adesina Apr 25 '18 at 08:51
2

Another approach to force a link to trigger a download in the browser is to add a download attribute like:

<a href="@Url.ContentUrl(Model.PdfMedia)" download>

This isn't Episerver-specific. More info: https://www.w3schools.com/tags/att_a_download.asp

Ted Nyberg
  • 7,001
  • 7
  • 41
  • 72