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

2

On the server side, is it possible to get full path of a file sent by a client using the <input type="file"> tag?

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

    
asked by anonymous 13.05.2014 / 22:03

2 answers

7

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

The maximum you'll 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 maximum you will get on the server is Teste.txt .

    
13.05.2014 / 22:45
8

After several changes to the initial proposal of the question I have to say that it is not possible to get the path from a local file in a browser on a service that is on a server other than by a security failure in any particular version, but this, in no case, should be considered. Even though this service is on the same machine as the browser, it is not possible without such crazy intervention that there are other, easier and more correct ways 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 resolve this problem? Does not make sense. One should use the best tool for the problem. Do not attempt to adapt the problem to the tool. We can not look at a way to solve the problem, we must find a solution to the problem.

With the indications I have, especially 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 placed it on 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 placed it on GitHub for future reference .

This does exactly what you need, as far as you can understand. 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 move_uploaded_files .

    
13.05.2014 / 23:12