0

用于将文件转换为 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()));
4

0 回答 0