ASP.NET File Upload Control Question

Posted on 2011-10-18
Last Modified: 2012-06-27
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
Question by:jazzcatone
    LVL 2

    Accepted Solution

    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.
    LVL 2

    Assisted Solution

    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.
    LVL 20

    Assisted Solution

    You should be able to get what you're looking for with this simply enough:

    string fName = FileUpload1.FileName;
    LVL 17

    Assisted Solution

    by:Carlos Villegas
    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


    Author Closing Comment


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now