In this article I will explain with an example, how to get full path from uploaded file in ASP.Net MVC.
The Misunderstanding
The 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
Initially, the full path of the File on the User’s machine was sent to the server, but later due to security reasons browsers is returning 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.
Solution
Namespaces
You will need to import the following namespace.
Controller
The Controller consists of following Action methods.
Action Method for handling GET operation
Inside this Action method, simply the View is returned.
Action Method for handling POST operation
This Action method accepts HttpPostedFileBase as parameter.
Note: The name of the HttpPostedFileBase parameter and the name of HTML FileUpload element must be exact same, otherwise the HttpPostedFileBase parameter will be NULL.
Inside this action method, first a check is performed whether Directory (Folder) exists if not then the Directory (Folder) is created using CreateDirectory method of Directory class.
Then, the file name is extracted from the FileName property of the HttpPostedFileBase class using the GetFileName function of the Path class.
Finally, the uploaded file is saved into the Folder (Directory) named Uploads using SaveAs method of HttpPostedFileBase class and the View is returned.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase postedFile)
{
if (postedFile != null)
{
string filePath = Server.MapPath("~/Uploads/");
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
string fileName = Path.GetFileName(postedFile.FileName);
postedFile.SaveAs(filePath + fileName);
}
return View();
}
}
View
HTML Markup
The HTML Form has been created using the Html.BeginForm method which accepts the following parameters.
ActionName – Name of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
HtmlAttributes – This array allows to specify the additional Form Attributes. Here we need to specify enctype = “multipart/form-data” which is necessary for uploading Files.
The Form also consists of an HTML5 FileUpload element and a Submit Button.
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="postedFile" />
<input type="submit" value="Upload" />
}
</body>
</html>
Screenshot
Downloads