1

How to make upload the image with dio in flutter?

  void _postHttp() async {
try {
  Dio dio = Dio();
  dio.options.contentType = Headers.formUrlEncodedContentType;
  FormData formData = new FormData.fromMap({
    "file": await MultipartFile.fromFile("/Users/rogerio/Downloads/tshirt_view_kovi.jpg",
      filename: "tshirt_view_kovi.jpg"),
    "type": "checklist",
    "source": "0bef60ed-f538-4a35-b5ef-70f7dfaf510e"
  });
  Response response = await dio.post(
      "https://api.kovi.dev/docs/upload-generic",
      data: formData,
      options: Options(contentType: Headers.jsonContentType));
  print(response);
} catch (e) {
  print(e);
}

}

My code return error 502

  • 1
    Does this answer your question? [How to upload image in Flutter?](https://stackoverflow.com/questions/44841729/how-to-upload-image-in-flutter) – benbotto Jan 22 '20 at 22:13

1 Answers1

0

Why do you set contentType to json? it should be multipart: multipart/form-data. Try this snippet without any options:

final dio = Dio();

final formData = FormData.fromMap({
  "file": await MultipartFile.fromFile(
    "/Users/rogerio/Downloads/tshirt_view_kovi.jpg",
    filename: "tshirt_view_kovi.jpg",
  ),
  "type": "checklist",
  "source": "0bef60ed-f538-4a35-b5ef-70f7dfaf510e"
});

final response = await dio.post(
  "https://api.kovi.dev/docs/upload-generic",
  data: formData
);
Andrey
  • 66
  • 6