locked
getting the full file path from a FileUpload control RRS feed

  • Question

  • User2069758653 posted

    When I browse to select a file using the FileUpload control, the file's full path is displayed in the accompanying textbox but when I try to get at the full path in my code, the path to the file is stripped off automatically and i can't find it.  I need to perform different actions depending on what directory the file is from, so i need to get at the path.  I seem to run into the same problem with the simple html file input control as well.

    It seems like I must be overlooking something very simple.  Does anyone know how I can get at the full path?

     thanks so much,
       Rob
     

    Tuesday, February 20, 2007 6:59 PM

Answers

  • User1439985827 posted
    You can't. For security purposes, the browser will never post the full file's path.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 20, 2007 7:02 PM
  • User1477435862 posted
    When I browse to select a file using the FileUpload control, the file's full path is displayed in the accompanying textbox but when I try to get at the full path in my code, the path to the file is stripped off automatically and i can't find it.  I need to perform different actions depending on what directory the file is from, so i need to get at the path.  I seem to run into the same problem with the simple html file input control as well.

    It seems like I must be overlooking something very simple.  Does anyone know how I can get at the full path?

     thanks so much,
       Rob

    As Kevin has said, you can't get the path of the file. The reason for this is that the path is only relative to the client computer and once the file is uploaded to the server it is no longer relevant. You can't depend on knowing the file structure of the client machine, as it may change without you knowing, which would then break your code. You will need some other method of determining what action to perform. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 20, 2007 11:07 PM
  • User-1087479560 posted
    Hi,

    Whether the full path of the posted file will be trimmed or not depends on the browser.
    If you are using ie, the path will not be trimmed, while for opera, only the file name is retrievable.

    The work around is to store the full path in somewhere else and then retrieve it on the server. You can use a hidden Textbox to store it, and use TextBox.Text on the server to get it.

    Hope it helps.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 26, 2007 11:05 PM

All replies

  • User1439985827 posted
    You can't. For security purposes, the browser will never post the full file's path.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 20, 2007 7:02 PM
  • User1477435862 posted
    When I browse to select a file using the FileUpload control, the file's full path is displayed in the accompanying textbox but when I try to get at the full path in my code, the path to the file is stripped off automatically and i can't find it.  I need to perform different actions depending on what directory the file is from, so i need to get at the path.  I seem to run into the same problem with the simple html file input control as well.

    It seems like I must be overlooking something very simple.  Does anyone know how I can get at the full path?

     thanks so much,
       Rob

    As Kevin has said, you can't get the path of the file. The reason for this is that the path is only relative to the client computer and once the file is uploaded to the server it is no longer relevant. You can't depend on knowing the file structure of the client machine, as it may change without you knowing, which would then break your code. You will need some other method of determining what action to perform. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 20, 2007 11:07 PM
  • User2069758653 posted

    In this situation, the page is part of our company's intranet, and there's a standard set of named shared directories for everyone.  If the file uploaded comes from the H drive, handle it one way, if it comes from the V drive, handle it another, and so on.  The directory structure is known beforehand.

    I figured out the basis of a work-around:

    A CustomValidator control runs a client-side script in which I set the value of an html element to the full path.  I' know how to set the innerHTML of an element in javascript, but I don't know how to read in that value in a server method (or even if that's possible ).  I also am not sure how/if it's possible to set the vale of a .NET element using javascript.

    Any ideas on either of these or maybe about some other kind of potential work-around?
     

     

    Wednesday, February 21, 2007 5:41 PM
  • User-1087479560 posted
    Hi,

    Whether the full path of the posted file will be trimmed or not depends on the browser.
    If you are using ie, the path will not be trimmed, while for opera, only the file name is retrievable.

    The work around is to store the full path in somewhere else and then retrieve it on the server. You can use a hidden Textbox to store it, and use TextBox.Text on the server to get it.

    Hope it helps.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 26, 2007 11:05 PM
  • User1158023974 posted

     Can you please explain that. Sorry I am very new. I need to pass the full file path as an argument to a function. How can I do it. In IE I will get the full file path but not in Mozilla

    Please Help

    Regards

    Hanimv 

    Friday, October 19, 2007 2:27 AM
  • User-1087479560 posted

     You can use javascript to achieve this before submit.

    For instance<asp:button onClientClick="document.getElementById('hiddenField1').value = document.getElementById('fileUpload1').value"  ... 

    Then the path can be retrieved on server side via hiddenField1.Value 

    Friday, October 19, 2007 3:12 AM
  • User1158023974 posted

    Thank you very much Wen . That helped 

    Saturday, October 20, 2007 6:34 AM
  • User28029742 posted

    There is another thread that's pretty similar to this that has another solution that does not require a hidden field.  http://forums.asp.net/p/1170634/1959688.aspx

    it uses the PostedFile piece of the FileUpload control.

    Thursday, October 25, 2007 12:23 PM
  • User269897107 posted

     I am using the hidden textbox, but still in Firefox I get only the file name. It works in anyway in IE...but not in Firefox. Is there anyway that accross different browsers I could just get the path and be able to save it in a variable of any kind?

    This is the way I am using it:

    ID="SendBtn" onClientClick="document.getElementById('HiddenTextBox').value = document.getElementById('AttachFileUpload').value" runat="server"...

     Thanks in advance!

    Monday, October 6, 2008 12:14 PM
  • User1648218563 posted

    I Solved the Problem

     lbltext.Text = FileUpload1.PostedFile.FileName

    Friday, February 13, 2009 12:51 AM
  • User-2059028967 posted

    Hi all!

    I got problem with full path of FileUpload control:

        When i use Attachment object in order to attach one file to EmailMessage. An error was occured " file not found " - Fileupload control only has a short filename not full path and even if it has full path, the file still not exist . Please help me! How to attach a file to EmailMessage?

     Thanks all!

    Wednesday, March 25, 2009 5:38 AM
  • User1116733743 posted

    Thanks, this helped me.

    lbltext.Text = FileUpload1.PostedFile.FileName 

    The reason I wanted this, was to import a set of data using excel sheet. I did not want to upload the file to server and then read it. For now it is working fine. I am able to read it from the local path and then import the data. Not sure if this will work in the hosted env. Will there be any security issues in readin the contect diectly from the clint.

    Manju 

     

     

    Monday, April 6, 2009 3:22 AM
  • User-1757185021 posted

     

    I Solved the Problem

     lbltext.Text = FileUpload1.PostedFile.FileName

     

    Can you pls elaborate on this. I didn't understand what you mean. I am facing a similar problem of getting the full path, not just the file name.

    Friday, June 5, 2009 3:43 PM
  • User-156926311 posted

    FileUpload1.PostedFile.FileName -> this actually returns the client filename. Meaning the path to the specified file.

    Supposed you have a file text1.txt stored in your C:/ drive.

    Selecting it in the FileUpload1 control and then accessing the FileUpload1.PostedFile.FileName attribute, would return:

    C:\\text1.txt

    Wednesday, November 4, 2009 12:29 PM