首页 > 解决方案 > 如何在 Flutter 中获取文件的 base64

问题描述

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

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

现在我想将文件转换成base64

我怎么能做到这一点?

标签: flutterbase64docxflutter-file

解决方案


最后我想出了一个解决方案。问题是我们必须在转换之前从路径中获取实际文件。

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

推荐阅读