Valkyrie
Valkyrie

Reputation: 45

Retrieve path from a file in ASP.NET Core

I want to retrieve the path from a file but I can't find out how to retrieve the correct path. For example, when I upload a file from C:\Users\Valk\OneDrive\Desktop\Test.xlsx, then I want to retrieve this path from the variable file. I used ASP.NET Core 6 Web API.

[HttpPost]
public IActionResult Post(IFormFile file)
{
    var filesPath = Directory.GetCurrentDirectory();
    var fileName = Path.GetFileName(file.FileName);
    var filePath = Path.Combine(filesPath, fileName);

    return Ok();
}

Upvotes: 0

Views: 1093

Answers (1)

Victor
Victor

Reputation: 8950

The IFormFile does not provide the source file location on the client side.

Because of the action method is running on the server side and it makes no sense where the original file was located on the client side.

At the moment of execution of your public IActionResult Post(IFormFile file) method you will no longer have access to the client side.

Upvotes: 1

Related Questions