Get full URL to media file...

Ayo
Ayo
Vote:
 

Hi guys, 

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

@Model.CurrentBlock.DownloadPdfText


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?

#191227
Apr 24, 2018 11:12
Vote:
 

What do you mean that the /globalassets/PDf-- etc is not the url to the actual location? My guess is that it IS the url to the file in the Episerver media archive. What do you expect it to be?

#191243
Apr 24, 2018 16:00
Vote:
 

Same as my answer on StackOverflow... just append /download to the URL and a download will be foreced (for browsers who honor this).

#191248
Apr 24, 2018 16:48
Vote:
 

You can't link the file like that. You would need to have a controller or something which returns headers so browser know it should download the file. One option is to make the link to an action which returns FileResult, passing the content guid. 

#191249
Edited, Apr 24, 2018 16:50
Vote:
 

/download will set those headers that will trigger a download.

#191250
Apr 24, 2018 16:52
Vote:
 

Sidenote; if you append /thumbnail (or the name of another thumbnail property in your content/media type) to an image URL you will get the thumbnail.

#191252
Apr 24, 2018 16:54
Ayo
Vote:
 

Johan Petersson - Your answer was what I was actually after, I only wanted the "REAL" url thinking that would change the way the browser deals with it....

so adding /download worked but what I want to know is why... is that an episerver thing or just a genric thing you can do with PDFs?

#191256
Apr 24, 2018 17:49
Vote:
 

Why is because Episerver sets the correct http response headers when you append /download. E.g.:

Content-Disposition: attachment; filename="filename.pdf"
#191258
Apr 24, 2018 19:16
Vote:
 

You also might want to set the download attribute on the anchor, please see https://www.w3schools.com/TAGS/att_a_download.asp

#191260
Apr 24, 2018 19:18
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.