如何将任何类型的文件转换为 base64 并且转换后的文件应该使用 Flutter 上传到 REST API?

问题描述

用于将文件转换为 base64

RaisedButton(
   shape: RoundedRectangleBorder(
        borderRadius:
            BorderRadius.circular(10.0),side: BorderSide(
          color: HexColor("#7733FF"),)),onpressed: () async {
      File pickedFile =
          await FilePicker.getFile(
              allowedExtensions: [
            'docx'
          ],type: FileType.custom);
      setState(() {
        govtId = pickedFile;
        var str = '$govtId';
        str = str.replaceAll("'"," ");
        var n = str.lastIndexOf('/');
        govtIdFileName =
            str.substring(n + 1);
      });

      File imageFile = new File(
          pickedFile.toString());
      List<int> imageBytes =
          imageFile.readAsBytesSync();
      base64Image =
          base64.encode(imageBytes);
      print(
          "***************base64******************");
      // ignore: unnecessary_brace_in_string_interps
      print("${base64}");
    },child: Text(
      'Upload',style: TextStyle(
          color: HexColor("#7733FF"),fontSize: 20,fontWeight:
              FontWeight.normal),),)

但我无法将该文件转换为 base64。我哪里做错了?

上传文件到 Rest API 的代码

final response = await http.post(apiUrl,headers: <String,String>{
    'Content-Type': 'application/json; charset=UTF-8'
  },body: jsonEncode(sendDocumentData.toJson()));

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)