In this article I will explain with an example, how to get full path from FileUpload control in ASP.Net using C# and VB.Net.
The misunderstanding is that developers tend to access the path of the file on the user’s machine that he has selected for upload in browsers like Internet Explorer IE, FireFox, Chrome, Safari and Opera. It is displayed as fakepath.
Initially in the early days, the full path of the File on the User’s machine was sent to the server but later due to security reasons browsers will return only file name instead of full path from client machine, as the path was being used to hack the computer.
Hence, now the location of the folder of the file on the user’s machine is not sent to the server and there is no need for the location of the file i.e. its full path for saving.
When the Upload Button is clicked, the filename is extracted from the FileName property using the GetFileName function of the Path class.
Then the uploaded File is saved into the Uploads Folder (Directory) using SaveAs method.
protected void btnUpload_Click(object sender, EventArgs e)
string fileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Uploads/" + fileName));
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim fileName As String = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)
FileUpload1.PostedFile.SaveAs(Server.MapPath(("~/Uploads/" + fileName)))