Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • cole young 20 posts 41 karma points
    Oct 06, 2017 @ 16:27
    cole young
    0

    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 444 posts 1544 karma points
    Oct 06, 2017 @ 18:09
    bh
    0

    I'd just get my hands dirty in the web.config

    <system.webServer>
        <rewrite>   
            <rewriteMaps>
                <rewriteMap name="StaticRedirects">
                    <add key="/YourAliasHere" value="/FolderName/FileName.pdf" />
                </rewriteMap>
            </rewriteMaps>
            <rules>
    
                <!--STATIC 301-->
                <rule name="Redirect Rule" enabled="true" stopProcessing="true">
                    <match url=".*" />
                    <conditions>
                        <add input="{StaticRedirects:{REQUEST_URI}}" pattern="(.+)" />
                    </conditions>
                    <action type="Redirect" url="http://www.yourdomainname.com{C:1}" appendQueryString="False" redirectType="Permanent" />
                </rule>
            </rules>
        </rewrite>
      </system.webServer>
    
  • cole young 20 posts 41 karma points
    Oct 06, 2017 @ 19:05
    cole young
    0

    @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.

  • Tobias Klika 101 posts 570 karma points c-trib
    Oct 07, 2017 @ 10:59
    Tobias Klika
    0

    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:

    1. Add a urlAlias property to your PDF document type.
    2. in web.config: <add verb="GET" path="pdf/*" type="My.Handlers.PdfHttpHandler, My.Assembly"/>
    3. Get the current request path in the ProcessRequest method of your handler.
    4. Use the last part of the path (your desired file name) to find a matching document with the same urlAlias.
    5. 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);

  • cole young 20 posts 41 karma points
    Oct 09, 2017 @ 14:27
    cole young
    0

    @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.

  • cole young 20 posts 41 karma points
    Oct 09, 2017 @ 15:05
    cole young
    0

    @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?

  • Nik 1612 posts 7258 karma points MVP 7x c-trib
    Oct 09, 2017 @ 15:50
    Nik
    0

    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

  • cole young 20 posts 41 karma points
    Oct 09, 2017 @ 16:04
    cole young
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft