title: how to upload image to rest API in flutter through http post method?
body: <p>I'm trying to upload an image through the flutter via post method. and I'm using image_picker for pick file from mobile but I can't able to upload</p>
  
  <p>and I have tried to send the file like FormData that also doesn't work</p>
  
  <pre><code>      Future&lt;dynamic&gt; uploadLicence(int id ,dynamic obj) async {
            FormData formdata = new FormData(); // just like JS
            formdata.add("image",obj); 
            final response = await post('Logistic/driver/LicenceImage? 
            driverId=$id', 
            formdata);
            print(response);
           //  return null;
       if (response.statusCode == 200) {
           final result = json.decode(response.body);
           return result;
      } else {
            return null;
     }
     }
  </code></pre>
  
  <p>after that, I just tried with this method but this also not working </p>
  
  <pre><code>   Future&lt;dynamic&gt; uploadLicence(int id, File file) async {
     final url = Uri.parse('$BASE_URL/Logistic/driver/LicenceImage? 
     driverId=$id');
     final fileName = path.basename(file.path);
     final bytes = await compute(compress, file.readAsBytesSync());
  
     var request = http.MultipartRequest('POST', url)
     ..files.add(new http.MultipartFile.fromBytes(                                                     
    'image',bytes,filename: fileName,);
    var response = await request.send();
    var decoded = await 
    response.stream.bytesToString().then(json.decode);
    if (response.statusCode == HttpStatus.OK) {
    print("image uploded $decoded");
    } else {
     print("image uplod failed ");
    }
    }
  
               List&lt;int&gt; compress(List&lt;int&gt; bytes) {
               var image = img.decodeImage(bytes);
               var resize = img.copyResize(image);
              return img.encodePng(resize, level: 1);
              }
  </code></pre>