【问题标题】:status: 400, message: There is some trouble to proceed your action!, data: null状态:400,消息:执行您的操作时遇到问题!,数据:空
【发布时间】:2021-11-30 20:25:34
【问题描述】:

想从画廊或相机上传图像到 api 这是我的图像 api

Future  profileImageUser(String user_id, String image) async {

      String url = 'api';
      final response = await http.post(Uri.parse(url),
          body: {
            'user_id': user_id,
            'image': image
          }
      );
      var convertedDataJson = jsonDecode(response.body);
      return convertedDataJson;
    }

这是我如何获取用户 ID 和图像以通过 rest api 上传它。

  Future _imgFromCamera() async {
    final image = await ImagePicker().pickImage(source: ImageSource.camera);


    setState(() {
      _image = image;
      final bytes = File(_image!.path).readAsBytesSync();
      String img64 = base64Encode(bytes);
     print(img64);
    });
    SharedPreferences prefs = await SharedPreferences.getInstance();
    if (_formKey.currentState!.validate()) {
      
      var myId = prefs.getString('user_id');
      print(myId);
      var myProfile = _image;
      print(myProfile); 
      var rsp = await profileImageUser(
        myId.toString(),
        myProfile.toString(),
        
      );
      print(rsp);
       if (rsp['status'] == true) {
        print(rsp);
      } else {
        print('failed');
      }
    }

当我从画廊或相机上传图片时,它会打印如下的 id 和图片

I/flutter ( 6665): Instance of 'XFile'
I/flutter ( 6665): 111

但它也显示以下错误

{status: 400, message: There is some trouble to proceed your action!, data: null}

这是我的邮递员 api 参数

键:user_id 价值:76 关键:图像 值:data:image/jpeg;base64,(和其他数据)

任何解决方案。

【问题讨论】:

    标签: flutter api http dart flutter-dependencies


    【解决方案1】:

    我基本上解决了这个问题,我没有正确转换 base 64 中的图像,你应该这样做

    Future createAlbum(String user_id , File image) async {
      final response = await http.post(
        Uri.parse('api'),
       /* headers: {
          'Accept': 'multipart/form-data'
        },*/
        body: {
          'user_id': user_id,
          'image': selectedImage != null ? 'data:image/png;base64,' +
              base64Encode(selectedImage!.readAsBytesSync()) : '',
        },
      );
    
      final responseJson = await json.decode(json.encode(response.body));
    
      print(responseJson);
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-29
      • 1970-01-01
      • 2023-03-07
      • 2012-12-07
      • 2015-03-04
      • 2020-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多