0

Possible Duplicate:
Get full path of a file with FileUpload Control

I want to get the path of a file when we select the file in file upload control.

eg. if my file is placed at C:\Documents and Settings\example.txt

when I browse it by using file upload control then how I will get this C:\Documents and Settings\example.txt path.

Community
  • 1
  • 1
Rajbir Singh
  • 1,641
  • 6
  • 23
  • 46
  • You can't get the client path; only the filename. This is by design from what I remember, a security feature. Note this is not true for all browsers though - see http://codeblog.shawson.co.uk/fileupload-postedfile-filename-includes-all-the-client-path-info-but-only-in-ie/ – dash Mar 30 '12 at 12:36

3 Answers3

3

Googling ASP.NET Upload Control yielded this as the first result:

protected void UploadButton_Click(object sender, EventArgs e)
{
    if(FileUploadControl.HasFile)
    {
        try
        {
            string filename = Path.GetFileName(FileUploadControl.FileName);
            FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
            StatusLabel.Text = "Upload status: File uploaded!";
        }
        catch(Exception ex)
        {
            StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }
}

EDIT - Now understanding your question fully, this is actually a question that has already been asked. Take a look at Get full path of a file with FileUpload Control and look at the highest upvoted answer.

Community
  • 1
  • 1
Code Maverick
  • 20,171
  • 12
  • 62
  • 114
  • Hey scott i know how to upload the file. But i do not know how to get the path which is this "C:\Documents and Settings\example.txt" from where the file is upload. – Rajbir Singh Mar 30 '12 at 12:36
  • Your are talking about how to get the name of the file. This "string filename = Path.GetFileName(FileUploadControl.FileName);" will give just file name not this ""C:\Documents and Settings\example.txt" path. – Rajbir Singh Mar 30 '12 at 12:41
  • Right, right, right. I misunderstood what you were trying to get. You can't get the client's path for privacy reasons. You just get the filename. – Code Maverick Mar 30 '12 at 12:42
2

Depending on the browser and OS your client is using, there may be no way for you to get the whole path - it's entirely up to what the browser supplies you.

According to this (see the bottom), IE on Windows XP gives the whole path, but on Windows 7 only gives the filename.

Only giving the filename is the expected behaviour.

Rawling
  • 49,248
  • 7
  • 89
  • 127
1

You can't, for security reason, you can get the fileName.

Use FileName property of the FileUploadControl

FileUploadControl.FileName

Read this blog: http://weblogs.asp.net/ashicmahtab/archive/2009/05/20/fileupload-control-doesn-t-give-full-path-help.aspx

Habib
  • 219,104
  • 29
  • 407
  • 436