Internet Explorer file uploads with ASP.NET

So you’d think that uploading a file using the standard FileUpload control would be easy. Well there is a small problem. Using the following code you would expect ‘filename’ to contain just the filename of the file uploaded.

String filename;
filename = FileUpload1.PostedFile.FileName;


And you would be right, if you’re not using Internet Explorer which seems to think that the path of the file is important too so it includes that.

Here’s a simple solution to get around this

String filename;
filename =
System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);

Now ‘filename’ will contain just the filename no matter which browser uploaded it.

Leave a Comment

Name

Mail (will not be published)

Website

Comment