helper
helper

Reputation: 97

IFormFIle not available in .Net core 5 class library

I can't use IFormFile in Class libraries.Net core 5, even after adding the package, Microst.aspnetcore.http.features

Upvotes: 0

Views: 983

Answers (1)

Jason Pan
Jason Pan

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.

enter image description here

Upvotes: 0

Related Questions