• 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
0
jazzcatone
Asked:
jazzcatone
4 Solutions
 
Paulmc999Commented:
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.

http://msdn.microsoft.com/en-us/library/7wtc81z6.aspx
0
 
Paulmc999Commented:
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.
0
 
ddayx10Commented:
You should be able to get what you're looking for with this simply enough:

string fName = FileUpload1.FileName;
0
 
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

0
 
jazzcatoneAuthor Commented:
Thanks
0

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