How to get the full Path of the file upload control in a string variable in asp .net?

15,708

Solution 1

You could try somehting like this : (where MyFileUploader is your FileUpload control)

                string fileBasePath = Server.MapPath("~/");
                string fileName = Path.GetFileName(this.MyFileUploader.FileName);
                string fullFilePath = fileBasePath + fileName;

Solution 2

It sounds like you're actually asking for the original path to the file on the client machine.

This is (a) useless (it's on a different computer) and (b) impossible to get (the browser doesn't tell you it).

What are you trying to do?

Share:
15,708
prabu R
Author by

prabu R

Updated on June 05, 2022

Comments

  • prabu R
    prabu R almost 2 years

    I would like to get the full path of the file upload control in the string variable. The file may be stored in any location other than the root of the project. Anybody please help out.

    The situation is:

    string file = Path.GetFileName(ExcelFileUpload.FileName);
                if (file.EndsWith(".xlsx"))
                {
                    // Reading from a binary Excel file (format; *.xlsx)
                    FileStream stream = File.Open(file, FileMode.Open, FileAccess.Read);
    
    • Laurent S.
      Laurent S. almost 11 years
      It's very unclear what you're trying to do. Please show us what you've tried and explain what you expect.
    • SLaks
      SLaks almost 11 years
      @Bartdude: My psychic debugging skills tell me that he actually wants to save the file, but doesn't realize that the bytes are sent to the server. (and may not realize that there are two machines involved)
    • Laurent S.
      Laurent S. almost 11 years
      I also think it's what he's trying to do, but who knows ? ;-)
    • prabu R
      prabu R almost 11 years
      I have updated the question with the situation where I want to apply the concept...
    • Igor
      Igor almost 11 years
      msdn has a straightforward example of what you are asking for: msdn.microsoft.com/en-us/library/…
    • SLaks
      SLaks almost 11 years
      @prabuR: My psychic debugging skills were correct. There is no local path to the file; it doesn't exist on the server. You can read from the POSTed body, though.
  • prabu R
    prabu R almost 11 years
    May I know what exactly is this.fileName and this.fullFilePath.
  • Igor
    Igor almost 11 years
    Just to mention - IE provides full client-side file path. And yes - it is meaningless for the server-side code.
  • Alicia
    Alicia almost 11 years
    Hi PrabuR , they are just variables, you can set it to any variable name you like i.e. string myFullPath = fileBasePath + this.fileName; the were prefixed with this. in my example as i had them created outside the method , but in your case you probably use a local variable. I'll update the answer.
  • SLaks
    SLaks almost 11 years
    @Igor: Not in recent versions, AFAIK.