how can you upload multiple images to a server using dio?

From the docs

FormData.fromMap({
    "files": [
      MultipartFile.fromFileSync("./example/upload.txt",
          filename: "upload.txt"),
      MultipartFile.fromFileSync("./example/upload.txt",
          filename: "upload.txt"),
    ]
  });

So in your case you will have something like this :

CreatePostImage(String author,List<File> images) async {
  try {
    String imagename;
    FormData formData = new FormData.fromMap({
      'author' : author.toString(),
      'MediaImageArray': images.map((item)=> MultipartFile.fromFileSync(item.path,
          filename: item.path.split('/').last)).toList()
});
 
    Response response = await Dio().post("$SERVER_IP/api/articles/", data: formData);
    print(response.toString());
  } catch (e) {
    print(e);
  }
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top