Upload Image From Flutter To C# REST Service

Viewed 19

I try to upload a image (XFile made with andoid cam) with Dio to my REST Service in C# with .net Core 6. What's the best practice without storing the image on the device?

I become an HTTP 415....

Flutter:

Uint8List imageBytes = await image.readAsBytes();

var response = await _client!.request('some URL',
        data: {"file": imageBytes},options: Options(method: 'POST', contentType: "image/jpeg"))

REST:

[HttpPost("uploadimage")]
public async Task<ActionResult> UploadImage(IFormFile file)
{
   // something special
}
1 Answers

You can use multipart and FormData to upload images as file to a rest service.

Here is an example code:

var dio = Dio();
    var formData = FormData.fromMap({
      'fichier': await MultipartFile.fromFile
(filePath, filename:'upload')
    });
var response = await dio.post(
'url.php', 
data: formData);
Related