如何在 Flutter 中获取文件的 base64

How to get base64 of a file in Flutter

我有一个文件路径字符串,如

path = /data/user/0/com.digitalpathshalabd.school/cache/Shaiful_Islam.docx

现在我想把文件转成base64

我怎样才能做到这一点?

终于想到了解决办法。 问题是我们必须在转换之前从路径中获取实际文件。

  1. 获取实际文件
  2. 将文件转换为字节数组
  3. 最终将字节数组转换为base64
import 'dart:convert';
import 'dart:io';

class FileConverter {
  static String getBase64FormateFile(String path) {
    File file = File(path);
    print('File is = ' + file.toString());
    List<int> fileInByte = file.readAsBytesSync();
    String fileInBase64 = base64Encode(fileInByte);
    return fileInBase64;
  }
}