skip to Main Content

I have List of images. and i need to upload that list of images from server using dio. and also need to upload using MultipartFile.fromFile in dio.
so, how can i upload it ?

'document': await MultipartFile.fromFile(file[index].path),

i try this…

for (int index = 0; index < file.length; index++)
      'document': await MultipartFile.fromFile(file[index].path),

formdata
but it’s not working. they upload only one image from list.
so is there any other way to upload list of images from server using dio flutter ?

2

Answers


  1. You can try by following code:

     try {
              FormData formData;
              List<dynamic>? documents = [];
              for (int i = 0; i < file.length; i++) {
                String? fileName = file[i]?.path.split('/').last;
                documents.add(await MultipartFile.fromFile(file[i]?.path ?? '', filename: fileName));
              }
              formData = FormData.fromMap({
                'files[]': documents,
              });
              final response = await _dio.post("/medias", data: formData);
              return response.data;
         }
    
    Login or Signup to reply.
  2. Inside your upload method:

    Map<String, dynamic> postData = Map<String, dynamic>();
    
    postData["attachment[]"] = [];
    
    for (File item in file) {
      postData["attachment[]"].add(await MultipartFile.fromFile(file.path, filename: basename(file.path)));
    }
    
    final formData = FormData.fromMap(postData);
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search