Copied to clipboard

Flag this post as spam?

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


  • Connie DeCinko 931 posts 1160 karma points
    Jan 12, 2011 @ 17:50
    Connie DeCinko
    0

    Umbraco URL Alias for Media/PDFs

    Has anyone come up with a good solution for creating short/friendly URLs for uploaded files, such as PDFs in the media gallery?  Currently if we want to provide a direct link to an uploaded PDF it must be in a format such as http://my.site.com/media/24550/mynamedfile.pdf.

     

  • Lee Kelleher 4026 posts 15837 karma points MVP 13x admin c-trib
    Jan 12, 2011 @ 18:16
    Lee Kelleher
    1

    Hi Connie,

    One suggestion could be...

    Create a new content page for each PDF.  The doc-type would have 2 properties: a media picker and the bit.ly URL shortener.

    The template for the doc-type would contain a redirect to the PDF... something like this:

    <%@ Master Language="C#" MasterPageFile="~/umbraco/masterpages/default.master" AutoEventWireup="true" %>
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            var currentNode = umbraco.presentation.nodeFactory.Node.GetCurrent();
            var mediaProperty = currentNode.GetProperty("mediaId");
            int mediaId;
            if (int.TryParse(mediaProperty.Value, out mediaId))
            {
                var media = new umbraco.cms.businesslogic.media.Media(mediaId);
                var pdf = media.getProperty("umbracoFile").Value.ToString();
    
                Response.RedirectPermanent(pdf, true);
            }
        }
    </script>

    In a nutshell, you use the bit.ly generated short-url to point to the content page... which redirects to the PDF.  It might seem a little long-winded, but it would work with current tools.

    Disclaimer: The code snippet hasn't been tested ... and could do with a little error checking (in case of no PDFs or Media nodes being deleted, etc).

    Cheers, Lee.

  • Connie DeCinko 931 posts 1160 karma points
    Mar 21, 2016 @ 17:41
    Connie DeCinko
    0

    Lee, this works great. But how would I make the PDF open in a new window? I seem to be stumbling on the correct format.

  • Connie DeCinko 931 posts 1160 karma points
    Mar 21, 2016 @ 18:15
    Connie DeCinko
    0

    As usual, answered my own question. Changed the Response.Redirect to:

    Response.Write("<script>window.open('" + pdf + "','_blank'); window.history.back();</script" + ">");
    

    Since you are technically navigating to another page, you have to go back to the previous page to give the appearance you never left it. Unless someone has a better idea.

  • Connie DeCinko 931 posts 1160 karma points
    Jan 26, 2011 @ 19:21
    Connie DeCinko
    0

    Seems like a lot of work and awfully redundant to have to upload the PDF and create a document for the PDF.  Although, I could then add extra meta data which would make it appear more friendly in search results.

    The other option would be a version of your code above, but a single content page that then looks up the short to long URL mapping in a database table.

     

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies