Home > other >  Upload images From Flutter To Core 6
Upload images From Flutter To Core 6

Time:10-11

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?

CodePudding user response:

I am using Dio to do this. This is the Frontend and the Backend for this

CodePudding user response:

[HttpPost("SampleFile")]
public async Task<IActionResult> SampleFile([FromForm] IFormFile file)
{

}


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

}
  • Related