0

I'm trying to upload multiple images in backend.

I got this response when I'm trying to print files:

[Instance of 'MultipartFile', Instance of 'MultipartFile', Instance of 'MultipartFile']

but at a server side I got null array {}. This is my method. I'm using http for api communication.

Future<Map<String, dynamic>> postWithMultiImage(
      String _url,
      Map<String, String> _headers,
      Map<String, String> _params,
      String _imageKey,
      List _imageFile) async {
    if (_headers != null) {
      print('_headers => $_headers');
    }
    print('_params => $_params');

    print('_url => $_url');
    var request = http.MultipartRequest("POST", Uri.parse(BASE_URL + _url));
    if (_headers != null) {
      request.headers.addAll(_headers);
    }
    if (_params != null) {
      request.fields.addAll(_params);
    }
    if (_imageFile != null) {
      for (int i = 0; i < _imageFile.length; i++) {
        final _type = lookupMimeType(_imageFile[i]);
        final _name =
            '${DateTime.now().toIso8601String()}.${_type.split('/').last}';

        final _partFile = http.MultipartFile(
            _imageKey,
           File(_imageFile[i]).openRead(),
            File(_imageFile[i]).lengthSync(),
            filename: _name);
        request.files.add(_partFile);
      }

      print('request files: ${request.files}');
    }
    var response = await request.send();
    final code = response.statusCode;
    print('response code => $code');
    final responseBody = await http.Response.fromStream(response);
    final body = responseBody.body;
    final jsonBody = json.decode(body);
    Map<String, dynamic> _resDic;
    if (code == 200) {
      _resDic = Map<String, dynamic>.from(jsonBody);
      _resDic[STATUS] = _resDic[SUCCESS] == 1;
    } else {
      _resDic = Map<String, dynamic>();
      _resDic[STATUS] = false;
      _resDic[IS_TOKEN_EXPIRED] = 0;
      _resDic[MESSAGE] = jsonBody[MESSAGE] != null
          ? jsonBody[MESSAGE]
          : 'Something went wrong';
    }
    _resDic[HTTP_CODE] = code;
    return _resDic;
  }

Thanks in advance.

Ujjawal Maurya
  • 462
  • 5
  • 17
Shailendra Rajput
  • 2,131
  • 17
  • 26
  • Does this answer your question? [How to upload images and file to a server in Flutter?](https://stackoverflow.com/questions/49125191/how-to-upload-images-and-file-to-a-server-in-flutter) – Ujjawal Maurya Feb 22 '22 at 16:45
  • This might help you: https://www.developerlibs.com/2020/07/flutter-upload-multipart-images-server.html – Ujjawal Maurya Feb 22 '22 at 17:34

1 Answers1

0

You can try >>THIS<<

Ujjawal Maurya
  • 462
  • 5
  • 17