김세림
김세림

Reputation: 311

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)
}

Upvotes: 4

Views: 20398

Answers (1)

Anran Zhang
Anran Zhang

Reputation: 7727

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.

Upvotes: 8

Related Questions