Friday, February 01, 2008

ASP.NET Fileupload

Call me lazy cuz I am deserved it. It shocked me a bit when a production code crash on my face. As part of my job today I have to train a few admin staffs on how to operate our newly invented site www.ncdd.gov.kh/jobs it works out quiet well on my machine cuz I dev, test & teach on Mozill Firefox. But it turn out that I don't perform enough exercise cuz I haven't test the site with IE yeah, cuz I haven't use IE for almost 3 years enough to forgot about it :)) anyway here is the story.

Do you know this guy System.Web.HttpPostedFile? yeah, he is my best friend when it comes to uploading file in ASP.NET. However you should know that he acted bit strange with browse in used.

Let say file1 is an instance of HttpPostedFile and if we ask him file1.FileName
Firefox return :
"Hello My Friend.doc"
Nice , isn't it?

IE return :
"C:\Document & Setting ....\user_name\desktop\Hello My Friend.doc"
WWOWO WHAT?! it slap me so hard to make me laugh out loud :))
Not Nice !
Anyway quick fix would be add a small detection on the file1.FileName
int lastDirSeparatorChar = file1.FileName.LastIndexOf(Path.DirectorySeparatorChar);

if (lastDirSeparatorChar > 0)
{
filename = file1.FileName.Substring(lastDirSeparatorChar + 1);
}
else
{
filename = file1.FileName
}
Lesson learn - test dev. site with IE as much as I hate "IE"