1

I am trying to upload multiple image in server using android application which is builds with flutter language. Here is the code which I am trying.

static Future postDataWithFile(......List<File> images) async {

Map<String, String> headers = {
    "Accept": "application/json",
    'Content-Type': 'application/json; charset=UTF-8',
    'Authorization': 'Bearer $token',
  };

var request = http.MultipartRequest('POST', Uri.parse(url));
  request.headers.addAll(headers);

request.fields['contact'] = insertVisitedPlace.contact;
request.fields['ownerName'] = insertVisitedPlace.ownerName;
request.fields['orgName'] = insertVisitedPlace.orgName;
request.fields['orgtype'] = insertVisitedPlace.orgType.toString();
request.fields['nextFollowup'] = insertVisitedPlace.nextFollowup ?? "";

..............................

Here I am use a List to store all image.

List<MultipartFile> allImagesAfterConvert = [];

images.forEach((image) { 
  allImagesAfterConvert.add(
    await http.MultipartFile.fromPath(
      'orgImages',
      image.path,
     )
  );
});

Send request to server to add all images. But In the server get only one image.

request.files.addAll(allImagesAfterConvert);




try {
   final response = await request
       .send()
       .timeout(Duration(seconds: timeoutSeconds), onTimeout: () {
     throw TimeoutException("Connection time out. Please try again");
   });

   return _isValidResponse(response) ? response : _error(response);
 } on SocketException {
   throw Failure("No Internet Connection");
 } on TimeoutException {
   throw Failure("Request time out");
 } on Error catch (e) {
   throw Failure(e.toString());
 }


}

1 Answer 1

1

Replace orgImages with orgImages[]

images.forEach((image) { 
  allImagesAfterConvert.add(
    await http.MultipartFile.fromPath(
      'orgImages[]’,  
      image.path,
     )
  );
});

This should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.