HttpPostedFile.FileName browser dependent behaviour
Even though these kind of variables must have some standards (may be it already has), there are differences in practise. Be careful with the FileName property of HttpPostedFile class when you want to work on a file being uploaded by visitors of your page because the FileName value that you will get on Internet Explorer and Firefox are different.
If your visitor uses Internet Explorer, you will get the local path to the file. E.g.: C:\somefile.txt. However, if your visitor uses Firefox, the FileName value that you will get will be only the file’s name. E.g.: somefile.txt.
To avoid problems, you may have the following check.
string filename = postedFile.FileName;
if (filename.IndexOf('\\') > -1)
{
filename = filename.Substring(filename.LastIndexOf('\\') + 1);
}
else if (filename.IndexOf('/') > -1)
{
filename = filename.Substring(filename.LastIndexOf('/') + 1);
}
| Print article | This entry was posted by Coskun SUNALI on 14 Dec 2008 - Sun at 17:05, and is filed under General. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 1 year ago
Here is another way to get file name from full path :
filename = System.IO.Path.GetFileName(postedFile.FileName);
about 1 year ago
Hi Selcuk,
Thank you for sharing this nice method. I did not even have any idea about that it was there :)
Regards.