首页 > 解决方案 > 如何通过Api上传多张图片

问题描述

我正在尝试通过 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);
  }

标签: flutterdartfile-upload

解决方案


推荐阅读