I have my WebAPI method ready to accept file as parameter shown below: (e.g. URI "https://localhost:44397/api/uploadfile"
[Route("api/[controller]")]
[ApiController]
public class UploadFileController : ControllerBase
{
[HttpPost]
public async Task<IActionResult> Post([FromForm] IFormFile file)
{
}
}
Am using console app to send file to this API method, below is my code:
public static void Send(string fileName)
{
using (var client = new HttpClient())
using (var content = new MultipartFormDataContent())
{
client.BaseAddress = new Uri("https://localhost:44397");
var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
var index = fileName.LastIndexOf(@"\");
var fn = fileName.Substring(index + 1);
fs.Position = 0;
var contentfile = new StreamContent(fs);
content.Add(contentfile, "file", fn);
var result = client.PostAsync("/api/uploadfile", content).Result;
}
}
I also checked with other clients (e.g. Postman) and it failed there too (so it seems like an issue in the server-side, rather than the console app).
Not sure what am I doing wrong here. Whenever I check my WebAPI method file parameter is always null.
Can anyone help me finding solution to this? I tried searching blogs to no avail. I may be doing something naive here.