FileUpload.PostedFile.Filename includes all the client Path info, but only in IE


This is a known problem documented on the MSDN – I’m sure I’ve been caught by this before so thought i would blog it!

Firefox and IE7.05XX treat the PostedFile.Filename property differently.

In Firefox, calling FileUpload.PostedFile.Filename will return the name of the file, with no path information.

In IE7, calling FileUpload.PostedFile.Filename will return the full path of the file + the filename.

e.g. if the client file is located at c:\My Documents\Test.xls, Firefox will return ‘Test.xls’, and IE7 will return ‘c:\My Documents\Test.xls’

Related posts:

  1. #1 by Bhalchandra on September 6, 2010 - 13:01

    Thnx buddy. I was struggling with this. Is theres any solution to this problem

  2. #2 by shawson on September 13, 2010 - 10:19

    There’s no proper fix, but you can work around it by just checking for slashes in the path that comes back, and clean it up if there is- I do this;

    HttpPostedFile f = control.PostedFile;
    var cleaned_filename = f.FileName.ToLower();
    // ie leaves the path data on the front, so get rid of that!
    if (cleaned_filename.Contains(“\\”))
    cleaned_filename = cleaned_filename.Substring(cleaned_filename.LastIndexOf(“\\”), cleaned_filename.Length + 1 – cleaned_filename.LastIndexOf(“\\”));

(will not be published)