如何在Flutter中获取文件的base64

问题描述

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

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;
  }
}