1

I have an file path string like

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

now I want to convert the file into base64

How could I achieve this ?

Saiful Islam
  • 1,151
  • 13
  • 22
  • https://stackoverflow.com/a/56201075/7924565 check this – Majid Ali Nov 12 '20 at 11:32
  • Does this answer your question? [How to encode and decode Base64 and Base64Url in Flutter / Dart](https://stackoverflow.com/questions/56201074/how-to-encode-and-decode-base64-and-base64url-in-flutter-dart) – ajay Nov 12 '20 at 11:48
  • thank you all now i am going to implementing yours suggested solutions and i will notify you soon. thank you. – Saiful Islam Nov 12 '20 at 11:49
  • @ajay I said I have a file path not image. but you guys provided me a link of how to convert image to base 64 how dose it will work? – Saiful Islam Nov 14 '20 at 06:18

1 Answers1

8

Finally I come up with a solution. The thing is we have to get the actual file from path before converting it.

  1. get the actual file
  2. convert the file into byte array
  3. finaly convert the byte array to 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;
  }
}
Saiful Islam
  • 1,151
  • 13
  • 22