Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the file path of IFormFile?

When I receive a file from C# to iformfile, how do I know the file path of that file? If I don't know, how do I set the path?

public async Task<JObject> files(IFormFile files){
string filePath = "";
var fileStream = System.IO.File.OpenRead(filePath)
}
like image 732
김세림 Avatar asked Aug 31 '25 21:08

김세림


1 Answers

In this document, some instructions on IFormFile are introduced:

Files uploaded using the IFormFile technique are buffered in memory or on disk on the server before processing. Inside the action method, the IFormFile contents are accessible as a Stream.

So for IFormFile, you need to save it locally before using the local path.

For example:

// Uses Path.GetTempFileName to return a full path for a file, including the file name.
var filePath = Path.GetTempFileName();

using (var stream = System.IO.File.Create(filePath))
{
    // The formFile is the method parameter which type is IFormFile
    // Saves the files to the local file system using a file name generated by the app.
    await formFile.CopyToAsync(stream);
}

After this, you can get the local file path, namely filePath.

like image 189
Richard Zhang Avatar answered Sep 03 '25 11:09

Richard Zhang