Getting a media node by file name Options
leedo
Posted: Monday, April 14, 2008 6:00:42 PM
Rank: Newbie

Joined: 4/9/2008
Posts: 12
Location: Chicago
I have trying to create a download page that will take a filename parameter and then forward the user on to that file. I am having some major problems using XPATH to find the correct media node, though. I have been trying something like this, though I suspect it is not the best approach:
Code:
<xsl:variable name="fileName" select="umbraco.library:RequestQueryString('filename')"/>
<xsl:value-of select="mediaCurrent/node[@name=$filename]">

Any help would be much appreciated.
drobar
Posted: Monday, April 14, 2008 6:07:15 PM

Rank: Umbracoholic

Joined: 9/8/2006
Posts: 1,432
Location: KY, USA
I don't quite understand the situation. If you know the filename, don't you also know its path?

When you insert a link via the rich text editor, you can select your file from the media section and you'll have the full path and not need to use a macro or the querystring at all.

Or do you hard-code the filename ("myresume.pdf") and then want to automatically find out where that file lives in the media tree? Of course, you'd have to be absolutely sure you never have two files with the same name or you might get the wrong one back.

If you can help us understand what you're trying to achieve I'm sure we can help you do it.

cheers,
doug.

MVP 2007-2009 - Official Umbraco Trainer for North America - Percipient Studios
leedo
Posted: Monday, April 14, 2008 8:35:27 PM
Rank: Newbie

Joined: 4/9/2008
Posts: 12
Location: Chicago
drobar wrote:
I don't quite understand the situation. If you know the filename, don't you also know its path?

When you insert a link via the rich text editor, you can select your file from the media section and you'll have the full path and not need to use a macro or the querystring at all.

Or do you hard-code the filename ("myresume.pdf") and then want to automatically find out where that file lives in the media tree? Of course, you'd have to be absolutely sure you never have two files with the same name or you might get the wrong one back.

If you can help us understand what you're trying to achieve I'm sure we can help you do it.

cheers,
doug.

The problem we are trying to solve is tracking requests for PDF files in Google Analytics (even people directly accessing the PDFs without a referrer). What our ideal solution looks like is this:

1. person sends a request for a pdf e.g. http://www.example.com/media/pdfs/test.pdf
2. we use url rewriting to turn the url into http://www.example.com/download_file.aspx?filename=test.pdf
3. this lets us track the PDF request on Analytics, and then we redirect to the actual PDF

And actually, by typing out these steps I have realized that we can just pass the full media path to the intermediate page. The only reason we initially avoided that is because the url could get very long.

Anyways, thanks for your time... and if you have any better suggestions that would be much appreciated.

edit: However, it would be nice to be able to access the media node based on the full path so that we can quickly find out the title and filesize.
leedo
Posted: Wednesday, May 07, 2008 6:53:57 PM
Rank: Newbie

Joined: 4/9/2008
Posts: 12
Location: Chicago
Here is the solution we came up with for this problem
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    xmlns:umbraco.library="urn:umbraco.library"
    exclude-result-prefixes="msxml umbraco.library">


<xsl:output method="xml" omit-xml-declaration="yes" />

<xsl:param name="currentPage"/>

<xsl:template match="/">

<xsl:variable name="file" select="umbraco.library:RequestQueryString('file')"/>

<p>
    You are attempting to download <strong>
    <xsl:value-of select="substring($file,8)"/></strong>.<br />
    If the download does not start automatically please click on the link below:
</p>
<p>
    <a href="/download/media{$file}" target="_blank">
        <xsl:value-of select="substring($file,8)"/>
    </a>
</p>
<iframe style="display:none">
    <xsl:attribute name="src">
        <xsl:value-of select="concat(string('/download/media'),$file)"/>
    </xsl:attribute>
    &nbsp;
</iframe>

</xsl:template>

</xsl:stylesheet>


We used some mod_rewrite magic to redirect all requests for pdfs, docs, and other downloadable files to the above XSLT macro which forces a download through an iframe that points to the following .net macro.

Code:

<%@ Control Language="C#" ClassName="ServeFile" %>
<%@ Import Namespace="System.IO" %>

<script type="text/C#" runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        String file = Request.QueryString["file"];
        String localFile;
        if (file != "")
        {
            localFile = Server.MapPath(file);
            if (File.Exists(localFile)) {
                FileInfo fileinfo = new FileInfo(localFile);
                Response.ClearHeaders();
                Response.ClearContent();
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("content-disposition", "attachment; filename=\"" + fileinfo.Name + "\"");
                Response.AddHeader("content-length", fileinfo.Length.ToString());
                Response.WriteFile(localFile);
                Response.End();
            }
        }
    }
</script>
drobar
Posted: Wednesday, May 07, 2008 7:16:26 PM

Rank: Umbracoholic

Joined: 9/8/2006
Posts: 1,432
Location: KY, USA
If I understand your code correctly, you're saving your downloadable files to a new folder on your website at /download/media.

This would require direct or FTP access to the server to upload the downloadable files, right? That would also mean that you aren't using the Media section of umbraco and lose the ability to insert links conveniently (that is, to remember the filename and path). I can see benefits to this approach, but also limitations.

You've got a nice solution here. To complete this thread, would you be interested in trying to solve the same problem but for files stored in the umbraco Media section? I think that would be a great help to people.

cheers,
doug.

MVP 2007-2009 - Official Umbraco Trainer for North America - Percipient Studios
leedo
Posted: Wednesday, May 07, 2008 7:47:07 PM
Rank: Newbie

Joined: 4/9/2008
Posts: 12
Location: Chicago
drobar wrote:
If I understand your code correctly, you're saving your downloadable files to a new folder on your website at /download/media.

This would require direct or FTP access to the server to upload the downloadable files, right? That would also mean that you aren't using the Media section of umbraco and lose the ability to insert links conveniently (that is, to remember the filename and path). I can see benefits to this approach, but also limitations.

You've got a nice solution here. To complete this thread, would you be interested in trying to solve the same problem but for files stored in the umbraco Media section? I think that would be a great help to people.

cheers,
doug.

Hi Doug,

Actually the /download/media/ path is rewritten to point to the .Net macro that I posted, passing in everything after the /download part of the path. This lets us use the Umbraco media section, which makes inserting media links very easy!

Here are my rewrite rules for those that are interested:
Code:
RewriteCond %{HTTP_HOST} (?:www.)?example.com [NC]
RewriteRule ^/media(/.*\.(?:pdf|doc|xls|ppt)) /download-file.aspx?file=$1 [NC,L]
RewriteRule ^/download/(.*) /serve-file.aspx?file=$1 [NC, L]
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.