【问题标题】:How to get base64 of a file in Flutter如何在 Flutter 中获取文件的 base64
【发布时间】:2021-02-24 09:50:38
【问题描述】:

我有一个类似的文件路径字符串

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

现在我想把文件转换成base64

我怎样才能做到这一点?

【问题讨论】:

标签: flutter base64 docx flutter-file


【解决方案1】:

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

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

【讨论】:

  • 你会不会碰巧知道如何让它在 Flutter Web 上工作,伙计?在那里,我收到此错误:错误:不支持的操作:_Namespace
  • 能否提供详细的错误信息?弄清楚会很有帮助。
  • 我的错误。在flutter web中必须使用html.File,没有io.File
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-17
  • 1970-01-01
  • 1970-01-01
  • 2021-04-30
  • 2021-10-13
  • 1970-01-01
相关资源
最近更新 更多