Reputation: 97
I can't use IFormFile in Class libraries.Net core 5, even after adding the package, Microst.aspnetcore.http.features
Upvotes: 0
Views: 983
Reputation: 21873
Below code useful to me.
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace UploadDemo.Controllers
{
public class DocumentUpload
{
public string Description { get; set; }
public IFormFile File { get; set; }
public string ClientDate { get; set; }
}
[ApiController]
[Route("")]
public class UploadController : ControllerBase
{
public UploadController()
{
}
[HttpGet]
public ActionResult<string> Hello()
{
return "Hello World!";
}
[HttpPost]
[Route("UploadDocument")]
public async Task<object> UploadDocument([FromForm] DocumentUpload docInfo)
{
IFormFile iff = docInfo.File;
string fn = iff.FileName;
var tempFilename = $@"c:\temp\{fn}";
using (var fileStream = new FileStream(tempFilename, FileMode.Create))
{
await iff.CopyToAsync(fileStream);
}
return Ok($"File {fn} uploaded. Description = {docInfo.Description} on {docInfo.ClientDate}");
}
}
}
It works for me.
Upvotes: 0