How to link to a PDF with an alias and not the filepath.
What is the umbraco-est way to expose a pdf in the media section with a descriptive alias as the url instead of the file path in the media section?
Umbraco 7+
My concern is there is an easy way to do this and I am just missing it.
Problem: if site visitor bookmarks the pdf and the media item is updated with a new file with a different filename, the favorite still points to the old file path.
Desired solution: Make an alias url that when clicked loads the pdf in the browser without changing the url in the browser to the path of the file.
I have used the node.umbracoFile and node.Url but both of those expose the download path of the file.
I have redirected to the file path in a controller, but that changes the url in the browser so they can't favorite the alias.
I have a working solution that uses two controllers, but i am curious to find out if i have over-complicated this and there is a built in way to do this in Umbraco.
Can anyone give me some direction on the best way to do this?
@bh - Thanks for the reply. That is one solution i hadn't thought of. It would require making entries in the web.config each time a pdf was added, changed, or deleted.
I was hoping for a solution that would allow some point and click in the umbraco client and it would just handle it.
Thanks for reply. That is one good looking solution. I'll give it try. Much simpler than mine.
I made a pdf doc type with a matching controller to hyjack the pdfs url. I used the api to get the media id. I then call a second controller which inherits UmbracoApiController and read the contents of the pdf, set the ContentType like your example, and returned the pdf contents in response.Contents.
If i am reading your solution correctly, it depends on the urls being after "pdf/" in their path.
This is still a solution but only if you insist on where they will be entered.
Otherwise you will have to update the web.config for each root where pdf doc type content maybe entered.
I did something a bit different on a site relatively recently to try and handle this sort of problem.. it's not a perfect solution though.
What I did was create a "download" controller that can handle download requests.
Then when I rendered out a link to a document instead of something like:
/media/1234/myfile.pdf
I rendered /downloads/getfile?1234
The 1234 was the media ID from the media item so then my downloads controller can jump in, find the file and return it. So if media item 1234 in the back office is updated with a new file, the downlaod controller will find the new one for you.
Nice solution. I appreciate you sharing it. Similar to what i did except i used a hyjacked route to have a descriptive url and to get the media id. i then call the download controller and pass it the media id.
How to link to a PDF with an alias and not the filepath.
What is the umbraco-est way to expose a pdf in the media section with a descriptive alias as the url instead of the file path in the media section?
Umbraco 7+
My concern is there is an easy way to do this and I am just missing it.
Problem: if site visitor bookmarks the pdf and the media item is updated with a new file with a different filename, the favorite still points to the old file path.
Desired solution: Make an alias url that when clicked loads the pdf in the browser without changing the url in the browser to the path of the file.
I have used the node.umbracoFile and node.Url but both of those expose the download path of the file.
I have redirected to the file path in a controller, but that changes the url in the browser so they can't favorite the alias.
I have a working solution that uses two controllers, but i am curious to find out if i have over-complicated this and there is a built in way to do this in Umbraco.
Can anyone give me some direction on the best way to do this?
I'd just get my hands dirty in the web.config
@bh - Thanks for the reply. That is one solution i hadn't thought of. It would require making entries in the web.config each time a pdf was added, changed, or deleted.
I was hoping for a solution that would allow some point and click in the umbraco client and it would just handle it.
You just need to add a new HTTP handler to your web.config (
system.web
/httpHandlers
/add
).In your handler you can basically do anything. For instance:
urlAlias
property to your PDF document type.<add verb="GET" path="pdf/*" type="My.Handlers.PdfHttpHandler, My.Assembly"/>
ProcessRequest
method of your handler.urlAlias
.Show either a 404 (if not found) or output the file like this:
HttpContext.Current.Response.AddHeader("Content-disposition", "inline");
HttpContext.Current.Response.AddHeader("Expires", "0");
HttpContext.Current.Response.AddHeader("Pragma", "cache");
HttpContext.Current.Response.AddHeader("Cache-Control", "private");
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.WriteFile(pdfFilePath);
@Tobias,
Thanks for reply. That is one good looking solution. I'll give it try. Much simpler than mine.
I made a pdf doc type with a matching controller to hyjack the pdfs url. I used the api to get the media id. I then call a second controller which inherits UmbracoApiController and read the contents of the pdf, set the ContentType like your example, and returned the pdf contents in response.Contents.
@Tobias,
If i am reading your solution correctly, it depends on the urls being after "pdf/" in their path.
This is still a solution but only if you insist on where they will be entered. Otherwise you will have to update the web.config for each root where pdf doc type content maybe entered.
Am i reading that right?
Hi Cole,
I did something a bit different on a site relatively recently to try and handle this sort of problem.. it's not a perfect solution though.
What I did was create a "download" controller that can handle download requests.
Then when I rendered out a link to a document instead of something like:
/media/1234/myfile.pdf
I rendered /downloads/getfile?1234
The 1234 was the media ID from the media item so then my downloads controller can jump in, find the file and return it. So if media item 1234 in the back office is updated with a new file, the downlaod controller will find the new one for you.
Nik
HI Nik,
Nice solution. I appreciate you sharing it. Similar to what i did except i used a hyjacked route to have a descriptive url and to get the media id. i then call the download controller and pass it the media id.
Thanks again.
is working on a reply...