Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Problem with file path

Problem with file path

Scheduled Pinned Locked Moved ASP.NET
visual-studiosysadminhelpquestion
9 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maxdd 7
    wrote on last edited by
    #1

    I'm trying to use a upload form to read a file. The problem is, I cant access full path of the file so I can read wherever he is. The form is something like this:

    <form id="Form1" enctype="multipart/form-data" runat="server">
    <tr>
    <td>Escolha ficheiro:</td>
    <td><input id="upl" type="file" runat="server"></td>
    </tr>
    <tr>
    <td colspan="2">
    <input type="button" id="upload" value="Inserir"
    OnServerClick="uploadClick" runat="server">
    </td>
    </tr>
    </form>

    The problem its here:

    string path;
         path = upl.Value;
        Response.Write(path);
    

    Using VS 2008 I can see that when I use "Value" I am "acessing full file path on client's computer". Its exactly what I want but I'm getting on the output just the name of the file. Its very strange because also when I browse the file the textfield shows the full path. Also, if I change the type="file" to type="text" and then I input the path manually, everything works right. What am I doing wrong?

    C 1 Reply Last reply
    0
    • M Maxdd 7

      I'm trying to use a upload form to read a file. The problem is, I cant access full path of the file so I can read wherever he is. The form is something like this:

      <form id="Form1" enctype="multipart/form-data" runat="server">
      <tr>
      <td>Escolha ficheiro:</td>
      <td><input id="upl" type="file" runat="server"></td>
      </tr>
      <tr>
      <td colspan="2">
      <input type="button" id="upload" value="Inserir"
      OnServerClick="uploadClick" runat="server">
      </td>
      </tr>
      </form>

      The problem its here:

      string path;
           path = upl.Value;
          Response.Write(path);
      

      Using VS 2008 I can see that when I use "Value" I am "acessing full file path on client's computer". Its exactly what I want but I'm getting on the output just the name of the file. Its very strange because also when I browse the file the textfield shows the full path. Also, if I change the type="file" to type="text" and then I input the path manually, everything works right. What am I doing wrong?

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      I would say that you have no idea how ASP.NET works. Assuming you got the full path to the file on the client's machine, what would you hope to do with it ?

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      M 1 Reply Last reply
      0
      • C Christian Graus

        I would say that you have no idea how ASP.NET works. Assuming you got the full path to the file on the client's machine, what would you hope to do with it ?

        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

        M Offline
        M Offline
        Maxdd 7
        wrote on last edited by
        #3

        Do .txt data to SQL table operation. I'm not doing upload operation, I'm doing insert operation using upload form. The client machine is always the user machine so there's no problem. The following line its: string[] data = File.ReadAllLines(path); If there's another (and better) way, please say to me.

        C 1 Reply Last reply
        0
        • M Maxdd 7

          Do .txt data to SQL table operation. I'm not doing upload operation, I'm doing insert operation using upload form. The client machine is always the user machine so there's no problem. The following line its: string[] data = File.ReadAllLines(path); If there's another (and better) way, please say to me.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Why are you using pure HTML and not server controls ? Why not use a server control and just grab the file data from the stream you get passed ? Surely you're not saying your application is only ever used by someone who is on the server ? If it's never used over the internet, or a LAN, why use ASP.NET at all ?

          Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

          M 1 Reply Last reply
          0
          • C Christian Graus

            Why are you using pure HTML and not server controls ? Why not use a server control and just grab the file data from the stream you get passed ? Surely you're not saying your application is only ever used by someone who is on the server ? If it's never used over the internet, or a LAN, why use ASP.NET at all ?

            Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

            M Offline
            M Offline
            Maxdd 7
            wrote on last edited by
            #5

            Old habits... What I meant to say is that the method I was trying to use works because the file its always on client side, so why not open and read data on client side and write it on server side? I work on ASP.NET less than 48 hours so I dont know a server control to do that. By server control you mean something like this: ? <asp:FileUpload ID="FileUpload1" runat="server" /> What would be the diference?

            C 1 Reply Last reply
            0
            • M Maxdd 7

              Old habits... What I meant to say is that the method I was trying to use works because the file its always on client side, so why not open and read data on client side and write it on server side? I work on ASP.NET less than 48 hours so I dont know a server control to do that. By server control you mean something like this: ? <asp:FileUpload ID="FileUpload1" runat="server" /> What would be the diference?

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              Maxdd 7 wrote:

              so why not open and read data on client side

              Because your C# code never runs on the client, it runs on the server.

              Maxdd 7 wrote:

              What would be the diference?

              This control has a property that contains the bytes of the file in question.

              Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

              M 1 Reply Last reply
              0
              • C Christian Graus

                Maxdd 7 wrote:

                so why not open and read data on client side

                Because your C# code never runs on the client, it runs on the server.

                Maxdd 7 wrote:

                What would be the diference?

                This control has a property that contains the bytes of the file in question.

                Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                M Offline
                M Offline
                Maxdd 7
                wrote on last edited by
                #7

                I followed your advice and done:

                caminho = Path.GetFullPath(FileUploadControl.PostedFile.FileName);

                and

                cam = Path.GetFullPath(FileUploadControl.PostedFile.FileName);

                and another tries, and always have same result: C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\file.txt (is not even the same hard drive) I googled about it, and looks like File Upload Control does not allow getting full path of client's machine. They say its a security thing.

                C 1 Reply Last reply
                0
                • M Maxdd 7

                  I followed your advice and done:

                  caminho = Path.GetFullPath(FileUploadControl.PostedFile.FileName);

                  and

                  cam = Path.GetFullPath(FileUploadControl.PostedFile.FileName);

                  and another tries, and always have same result: C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\file.txt (is not even the same hard drive) I googled about it, and looks like File Upload Control does not allow getting full path of client's machine. They say its a security thing.

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #8

                  You didn't remotely follow my advice. you CANNOT access the file from the server to the client. The PostedFile property has a property which contains the file contents.

                  Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                  M 1 Reply Last reply
                  0
                  • C Christian Graus

                    You didn't remotely follow my advice. you CANNOT access the file from the server to the client. The PostedFile property has a property which contains the file contents.

                    Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                    M Offline
                    M Offline
                    Maxdd 7
                    wrote on last edited by
                    #9

                    You are right. I found this. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.postedfile.aspx[^] I've adapted to my program, but having the problem I tought. If I use streams to read the file, I will not have text data. So I'm trying convert that stream data using:

                    StreamReader reader = new StreamReader( myStream );
                    string text = reader.ReadToEnd();

                    and

                    byte[] bytes = new byte[myStream.Length];
                    string data = Encoding.ASCII.GetString(bytes);

                    But its not working :^) EDIT: I did this and its finally working :-D

                    byte\[\] bytes = new byte\[myStream.Length\];
                        myStream.Position = 0;
                        myStream.Read(bytes, 0, (int)myStream.Length); 
                        string data = Encoding.ASCII.GetString(bytes); 
                    

                    modified on Thursday, November 12, 2009 1:05 PM

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups