如何通过Api上传多张图片

时间:2020-09-04 07:40:00

标签: flutter dart file-upload

我正在尝试通过Api上传多张图片,但我不了解如何发送列表,不过我可以上传一张图片。尝试了很多搜索但没有帮助,我也导入了multi_image_picker我可以选择图像,但是问题出在上传中。

  Future<Map<String, dynamic>> _uploadImage(File image) async {
   

    String value = '';
    SharedPreferences pref2 = await SharedPreferences.getInstance();
    value = pref2.getString("user_role");

    final mimeTypeData =
        lookupMimeType(image.path, headerBytes: [0xFF, 0xD8]).split('/');

    // Intilize the multipart request
    final imageUploadRequest = http.MultipartRequest('POST', apiUrl);

    // Attach the file in the request
    final file = await http.MultipartFile.fromPath('photo', image.path,
        contentType: MediaType(mimeTypeData[0], mimeTypeData[1]));
    // Explicitly pass the extension of the image with request body
    // Since image_picker has some bugs due which it mixes up
    // image extension with file name like this filenamejpge
    // Which creates some problem at the server side to manage
    // or verify the file extension


    imageUploadRequest.files.add(file);
    imageUploadRequest.fields['mobile'] = _mobileNo.text;

    imageUploadRequest.headers.addAll({
      'Content-Type': 'application/json',
      'Authorization': Constants.authToken,
    });

    var response = await imageUploadRequest.send();
    if (response.statusCode == 200) print('Done!');

    final respStr = await response.stream.bytesToString();

    return json.decode(respStr);
  }

0 个答案:

没有答案