In this article I will explain with an example, how to get file path from IFormFile in ASP.Net Core (.Net Core) Razor Pages.
Note: For beginners in ASP.Net Core (.Net Core 7) Razor Pages, please refer my article ASP.Net Core 7 Razor Pages: Hello World Tutorial with Sample Program example.
 
 

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

Razor PageModel (Code-Behind)

The PageModel consists of following Handler methods.

Handler method for handling GET operation

This Handler method left empty as it is not required.
 

Handler Method for handling POST operation

This Handler method accepts IFormFile as parameter.
Note: The name of the IFormFileparameter and the name of HTML FileUpload element must be exact same, otherwise the IFormFileparameter will be NULL.
 
Inside this Handler method, a check is performed whether Directory (Folder) exists if not then the Directory (Folder) is created using IWebHostEnvironment interface.
Note: For more details about IWebHostEnvironment interface, please refer my article Using IWebHostEnvironment in ASP.Net Core.
 
Then, the file name is extracted from the FileName property of the IFormFile class object using the GetFileName function of the Path class.
Note: For more details on how to access Static Files in Core, please refer my article Static Files (Images, CSS and JS files) in ASP.Net Core.
 
Finally, an object of FileStream class is created and using CopyTo method of IFormFile class the file is saved to the Directory (Folder).
public class IndexModel : PageModel
{
    private IWebHostEnvironment Environment;
 
    public IndexModel(IWebHostEnvironment _environment)
    {
        this.Environment = _environment;
    }
 
    public void OnGet()
    {
    }
 
    public void OnPostUpload(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);
        }
    }
}
 
 

Razor Page (HTML)

HTML Markup

Inside the Razor Page, the ASP.Net TagHelpers is inherited.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
Attributes – 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 Razor Page consists of an HTML5 FileUpload element and a Submit Button.
The Submit Button has been set with the POST Handler method using the asp-page-handler attribute.
Note: In the Razor PageModel, the Handler method name is OnPostUpload but here it will be specified as Upload when calling from the Razor HTML Page.
 
@page
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@model Get_File_Path_Core_Razor.Pages.IndexModel
@{
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <form method="post" enctype="multipart/form-data">
        <input type="file" name="postedFile" />
        <input type="submit" value="Upload" asp-page-handler="Upload" />
    </form>
</body>
</html>
 
 

Screenshot

Get file path from IFormFile in ASP.Net Core Razor Pages
 
 

Downloads