• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 472
  • Last Modified:

ASP.NET File Upload Control Question

This seems really simple. But yet I must ask ... I am using ASP.Net FileUpload control. How to get just the file name of the uploaded file ? Am currently using  

string fileName = FileUpload1.PostedFile.FileName

This returns the full path plus the filename  from my client machine. (Ex C:\User\Desktop\myfile.txt)  Any advice on how to get just the file name (myfile.txt) would be most appreciated
4 Solutions
Here is reference to string functions you could use, if you combine searching for the last '\' with getting the substring that follows you should get the name of the file.

BTW if the path is always the exact same and is known beforehand then you could search for that pattern in the string and replace it with nothing '' and then you are left with the file name!!

if you do not know the path in advance then you will have to search for the last '\' as i suggested above and then copy what comes after the last \ until you have the file name.
You should be able to get what you're looking for with this simply enough:

string fName = FileUpload1.FileName;
Carlos VillegasFull Stack .NET DeveloperCommented:
ddayx10 is correct, you just must use the FileName property of the System.Web.UI.WebControls.FileUpload class.

Also, when you want to extract only the file name of a path you can use this function, example:
string fileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);

Open in new window

jazzcatoneAuthor Commented:

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now