What Was I Thinking?

Follies & Foils of .NET Development
posts - 95 , comments - 352 , trackbacks - 0

QuickTip: Don’t rely on HTTPPostedFile.FileName

Supposedly, HTTPPostedFile.FileName is supposed to contain just the name of the uploaded file.  It should exclude the original filename path.  This doesn’t always appear to be the case, and seems to vary by the  browser doing an upload.

 

To avoid the browser issue entirely, use a FileInfo(HttpPostedFile.FileName) to return just the file name (and extension) portion of the file.

 

Change this:

Code Snippet
  1. var saveFileName = Path.Combine(UploadedFileServerPath, FilePackage.PostedFile.FileName);

to this:

Code Snippet
  1. var saveFileName = Path.Combine(UploadedFileServerPath, new FileInfo(FilePackage.PostedFile.FileName).Name);

Print | posted on Friday, November 5, 2010 8:57 PM | Filed Under [ Visual Studio ]

Feedback

Gravatar

# re: QuickTip: Don’t rely on HTTPPostedFile.FileName

Or
Path.GetFileName(FilePackage.PostedFile.FileName)
2/28/2014 9:53 AM | Path.GetFileName
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 

Powered by: