How to get base64 of a file in Flutter

Issue

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 ?

Solution

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

Answered By – Saiful Islam

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *