Getting the full path of a client file in ASP.NET

Asked

Viewed 4,464 times

3

On the server side, you can get full path of a file sent by a client using the tag <input type="file">?

For example, if the user sends the file C:\Users\Documents\a.txt, has how the server can get the string C:\Users\Documents\a.txt?

  • 1

    Have you tried System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName);? You can put what you tried or an excerpt you’re trying to do?

  • filename in case is the name="" that puts the right input tag ?

  • for now I just made the <input type="file" name"photo">

  • It may be, but it is a property that will have the name of the file that was loaded on the server. Have you seen this example? http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.postedfile(v=vs.110). aspx

  • Webforms, I only need to take the path of the selected file

  • IE takes the full customer path

Show 1 more comment

2 answers

8


From the server it is not possible to take the path of a file submitted by the client, due to security restrictions.

The most you will get is the file name, its size, extension and MIME.

For example, if the client submits the file C:\A\B\C\Teste.txt, the most you can get on the server is the name Teste.txt.

  • 1

    I think (or thought) he wants the path on the server, if it is on the client side, as you suggest, there is no way. I just thought about the way it works :)

  • Yeah, the question got a little vague... but I really think that’s it :)

  • yes, that’s right Rafa, how can I type PHP that has move_uploaded_files ?

  • 1

    @Enzotiezzi then, with the move_uploaded_files you take the file that was sent to the server and send it to a path that you want. It’s the opposite of this answer.

  • no, move_upload_files only copies the source path file to some wish path (which may be a server or not), that’s all I want to do

  • 1

    @Enzotiezzi You don’t understand what the bigown said, you’re saying the same thing...

  • then in case I only wish to do the same thing.

  • So @Enzotiezzi, if what you want is similar to the move_uploaded_files PHP, how this answer answers what you need?

  • as it would just be local, I do kind of a gambiarra, take the file path, use a process to run a COPY command to a desired folder

  • 1

    @Enzotiezzi is, I can not understand what you want. You speak on server, then speak on site. You say you want something like move_uploaded_files only that using ASP.Net, then says that will copy OS.

  • Next, I want to take a file and copy it to another folder, how is it done??? you take the path of the source file and make a COPY for the desired folder, that move_uploaded_file does, and that’s what I want to do on Asp.net, the move_uploaded_file works both local and server, so whatever the way, because at the end of the cases will be a user who will upload the file

Show 6 more comments

8

After several changes in the initial proposal of the question I have to say that it is not possible to pick up the path of a local file in a browser on a service that is on a server except for an eventual security breach in some specific version, but this under no circumstances should be considered. Even if this service is on the same machine as the browser, it is not possible without such a crazy intervention that there are other easier and more correct means to solve the problem, which is what matters.

If there is this guarantee that the server and browser are on the same machine, why use a browser to solve this problem? It makes no sense. One should use the best tool for the problem. Do not try to adapt the problem to the tool. We cannot set ourselves on a path to solve the problem, we must find a solution to the problem.

With the indications I have, mainly in comments, of how you are doing, the best I can post is this:

<form id="form1" runat="server">
    <asp:FileUpload id="FileUpload1" runat="server" />
    <asp:Button runat="server" id="btnUpload_Click" text="Upload" onclick="btnUpload_Click" />
    <br />
    <asp:Label runat="server" id="StatusLabel" text="Upload status: " />
</form>

I put in the Github for future reference.

Code Behind

protected void btnUpload_Click(object sender, EventArgs e) {
    string fileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
    FileUpload1.PostedFile.SaveAs(Server.MapPath("/Uploads/" + fileName));
}

I put in the Github for future reference.

This does exactly what you need, as far as you can see. Of course you will have to adapt to the specific characteristics of your code. But the heart of the matter is there.

See the documentation of the method which does the same as the move_uploaded_files.

  • If you read the file just use the FileUpload.FileContent thus using (StreamReader reader = new StreamReader(FileUpload.FileContent, System.Text.Encoding.UTF8))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.