Thursday, July 10, 2008

ASP.NET FileUpload Control Issue

I had an issue with file paths when using the ASP.NET 2.0 FileUpload Control that was giving me a fit for a little while. After some searching on Google, I gained a little more insight and discovered that if you use PostedFile that IE saves the file's complete path, which includes the path location of where the file was uploaded from in addition to the location you are saving files to on the server. If you use PostedFile with Firefox, Opera, or Safari, the file's complete path is trimmed to just the filename and the location you are saving files to on the server.

Instead of using for example:

string path = "/uploads/ + FileUpload1.PostedFile;

Use this:

string path = "/uploads/ + FileUpload1.Filename;

This will assign only the file's filename to the variable path, so if you are saving the paths of files in a database and upload files using IE the path will not also contain the location of the file on the client's machine as well as the location you are saving your files to on the server.

I had never used the FileUpload Control before until recently, so hopefully this will help anyone out using it for the first time.

1 comment:

Anonymous said...

great... i almost try to filter the complete path