asp.netflutterapiasp.net-core-6.0uploading

Upload images From Flutter To Core 6


I am Trying to upload images from flutter using API Post to server Asp.net core 6:

Flutter :

Future<String> ApiUploadImage(String fileURL)
{
    var uri =Uri.parse("https://..../.../UploadImages/PostImage/" );
    var request = http.MultipartRequest('POST', uri    );   
    request.files.add(await http.MultipartFile.fromPath('picture', fileURL));
    var res = await request.send();
    return res.toString();
}

Core 6 :

    [Route(".../[controller]/[action]/{id?}")]
    [ApiController]
    public class UploadImagesController : ControllerBase //ApiController
    {
        [HttpPost]
        public async Task<HttpResponseMessage> PostImage() 
        {

         }
      }

I also tried to use the following:

MultipartMemoryStreamProvider provider = await Request.Content.ReadAsMultipartAsync();

but It requires that Controller to be "ApiController" which doesn't work also.

My question : How to write server side code that will accept an image?


Solution

  • [HttpPost("SampleFile")]
    public async Task<IActionResult> SampleFile([FromForm] IFormFile file)
    {
    
    }
    
    
    [HttpPost("SampleFiles")]
    public async Task<IActionResult> SampleFiles([FromForm] List<IFormFile> files)
    {
    
    }