In this article I will explain with an example, how to get file path from IFormFile in ASP.Net Core (.Net Core) 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
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 IFormFile as parameter.
Note: The name of the IFormFile parameter and the name of HTML FileUpload element must be exact same, otherwise the IFormFile parameter will be NULL.
Inside this Action method, a check is performed whether Directory (Folder) exists if not then the Directory (Folder) is created using IWebHostEnvironment interface.
Then, the file name is extracted from the FileName property of the IFormFile class using the GetFileName function of the Path class.
Finally, an object of FileStream class is created and using CopyTo method of IFormFile class the file is saved to the Directory (Folder) and the View is returned.
public class HomeController : Controller
{
private IWebHostEnvironment Environment;
public HomeController(IWebHostEnvironment _environment)
{
this.Environment = _environment;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(IFormFile postedFile)
{
string filePath = Path.Combine(this.Environment.WebRootPath, "Uploads");
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
string fileName = Path.GetFileName(postedFile.FileName);
using (FileStream stream = new FileStream(Path.Combine(filePath, fileName), FileMode.Create))
{
postedFile.CopyTo(stream);
}
return View();
}
}
View
HTML Markup
The View consists of an HTML Form which has been created using the following TagHelpers attributes.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – 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.
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post" asp-controller="Home" asp-action="Index" enctype="multipart/form-data">
<input type="file" name="postedFile" />
<input type="submit" value="Upload" />
</form>
</body>
</html>
Screenshot
Downloads