Blogs tagged with 'User experience'

Dienstag, 26. Mai 2009

Replacing characters for filenames on upload

After I got tired to talk to our users that they should upload only web friendly named files and they don't care about it (or they don't understand it or perhaps they have something like Alzheimer) I added character replacing to the upload field of umbraco.

First I added a new section to the umbraco.config under content:

<content>
    <filenameReplacing>
        <char org=" ">-</char>
        <char org="&quot;"></char>
        <char org=";"></char>
        <char org="/"></char>
        <char org=":"></char>
        <char org="+">plus</char>
        <char org="*">star</char>
        <char org="&amp;"></char>
        <char org="?"></char>
    </filenameReplacing>
</content>

After that I added a new property to the UmbracoSettings class in the umbraco.businesslogic project:

/// <summary>
/// Gets a list of characters that will be replaced when uploadingFiles via fileUpload
/// </summary>
/// <value>The filename replacement characters for uploaded Files.</value>
public static XmlNode FilenameReplaceCharacters
{
    get { return GetKeyAsNode("/settings/content/filenameReplacing"); }
}

Finally I added some lines to uploadField class in the umbraco.editorControls project

if (umbraco.UmbracoSettings.UploadAllowDirectories)
{
    // Changed by TH to replace filename characters (2009-05-26)
    filename = this.getFilename(_text.Substring(_text.LastIndexOf("\\") + 1, _text.Length - _text.LastIndexOf("\\") - 1).ToLower());

    // Create a new folder in the /media folder with the name /media/propertyid
    System.IO.Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.Path + "/../media/" + _data.PropertyId.ToString()));
    _fullFilePath = System.Web.HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.Path + "/../media/" + _data.PropertyId + "/" + filename);
    this.PostedFile.SaveAs(_fullFilePath);
    _data.Value = "/media/" + _data.PropertyId + "/" + filename;
}
else
{
    // Changed by TH to replace filename characters (2009-05-26)
    filename = this.getFilename(System.IO.Path.GetFileName(this.PostedFile.FileName));

    filename = _data.PropertyId + "-" + filename;
    _fullFilePath = System.Web.HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.Path + "/../media/" + filename);
    this.PostedFile.SaveAs(_fullFilePath);
    _data.Value = "/media/" + filename;
}

and a new function:

/// <summary>
/// Function to get the filename with replaced characters
/// </summary>
/// <param name="originalFilename"></param>
/// <returns></returns>
private string getFilename(string originalFilename)
{
    string retVal = System.IO.Path.GetFileNameWithoutExtension(originalFilename);
    System.Xml.XmlNode replaceChars = UmbracoSettings.FilenameReplaceCharacters;
    foreach (System.Xml.XmlNode n in replaceChars.SelectNodes("char"))
        if (n.Attributes.GetNamedItem("org") != null && n.Attributes.GetNamedItem("org").Value != "")
            retVal = retVal.Replace(n.Attributes.GetNamedItem("org").Value, xmlHelper.GetNodeValue(n));
    return retVal + System.IO.Path.GetExtension(originalFilename); 
}

I recompiled the solution and replaced the businesslogic.dll, the umbraco.editorControls.dll and the umbracoSettings.config on the webserver.

This works fine for me and saves me a lot of time talking to the users uploading web friendly filenames.

Your feedback is welcome

Comments: 0  Add Comment Filed under: Umbraco | Tips and Tricks | User experience |

Archive


Subscribe Feed

Me on Twitter

09.07.10 20:02:  Aaaaadler ;-)

09.07.10 20:01:  Einmal Bier holen = Tor verpasst => Der Weg in den Keller ist eindeutig zu lang...

09.04.10 06:04:  @Shazwazza Done ;-) #ucomponents #Umbraco

09.03.10 20:43:  @netaddicts LOL

09.03.10 20:43:  @asp_net wie war das bei der 2010 Roadshow, Zitat: 'google das mal mit Bing'

About

It's me
This blog is written by Thomas Höhler. Living next to Frankfurt, Germany, I am trying to share my experiences about Umbraco [the most flexible CMS I know], my ClientTools for Umbraco, and some other more or less usefull stuff

XING

Latest Posts

Design & Masterpage by 69° media solutions