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.
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).
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.
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.
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.
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:
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.
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.
As usual, answered my own question. Changed the Response.Redirect to:
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.
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.
is working on a reply...