In this article I will explain a solution with an example, how to solve the issue of FakePath problem happening in browsers such as Chrome, FireFox, Internet Explorer (IE), etc., when selecting a File in ASP.Net FileUpload control.
This article will also explain why browsers such as Chrome, FireFox, Internet Explorer (IE), etc. display FakePath instead of the actual selected Path of the file in ASP.Net FileUpload control.
The Misunderstanding
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.
The Reason
Thus I would like to explain that initially in early days it was sent to the server but later on due to advancement in the Internet field, that 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.
Solution: You don’t need that path
Now there’s no use of the location of the file i.e. its complete path as new developers forget the fact that what they are developing is a client server application and it will be hosted on server. Since during development both the client and the server are same machine you tend to use the same path for saving. But that’s incorrect.
You need to save the file on the server’s folder and an easiest way would be to create a Folder called Uploads in your Website or Web Application and then saving the file there using the following code.
C#
protected void btnUpload_Click(object sender, EventArgs e)
{
string fileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Uploads/" + fileName));
}
VB.Net
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)))
End Sub