I'm having a problem with a number of users uploading pdf files into the media library with & in the filename then creating a link from the richtext editor that of course doesn't work.
Apart from educating them... would it be better to check with a Regex on the Media type or deal with it by using events?
Yup, I'd use events to handle this and use store what characters should be replaced by what other character (similar to what is now being used for constructing urls)
Here is a slightly cut down version of the event code I ended up using to catch this problem and rename the file and notify the user.
using System; using System.Web; using System.IO;
using umbraco.BusinessLogic; using umbraco.cms.businesslogic; using umbraco.cms.businesslogic.web; using umbraco.cms.businesslogic.media; using umbraco.cms.businesslogic.property; using umbraco.BasePages;
namespace ESCUmbracoEvents.media { public class filenames : ApplicationBase { public filenames() { Media.AfterSave += new Media.SaveEventHandler(Media_AfterSave); }
//check to see if there is a file if (!string.IsNullOrEmpty(fileprop.Value.ToString())) { string path = fileprop.Value.ToString();
if(path.Contains("&")) {
string safePath = path.Replace("&", "");
//copy the old file and rename System.IO.File.Copy(HttpContext.Current.Server.MapPath(fileprop.Value.ToString()), HttpContext.Current.Server.MapPath(safePath), true);
//delete the old file System.IO.File.Delete(HttpContext.Current.Server.MapPath(fileprop.Value.ToString()));
//set the file property to the new path fileprop.Value = safePath;
((BasePage)HttpContext.Current.Handler).speechBubble(BasePage.speechBubbleIcon.error, "Illegal Characters", "Illegal characters have been removed from the filename. Please reload this node to see the changes."); } } } } }
Pretty simple and I hope it helps someone else out.
Ampersand (&) in media filename
Hi
I'm having a problem with a number of users uploading pdf files into the media library with & in the filename then creating a link from the richtext editor that of course doesn't work.
Apart from educating them... would it be better to check with a Regex on the Media type or deal with it by using events?
Cheers
Evan
Yup, I'd use events to handle this and use store what characters should be replaced by what other character (similar to what is now being used for constructing urls)
Cheers,
/Dirk
Thanks Dirk
I have used a Regex validation on the 'File' Media Type as a quick solution.
To only allow alphanumeric PDF files with - or _ in the name.
I think I'll wait until tomorrow to look at the Events option now.
Cheers
Evan
Here is a slightly cut down version of the event code I ended up using to catch this problem and rename the file and notify the user.
Pretty simple and I hope it helps someone else out.
Cheers
Evan
is working on a reply...