fileSaveAs static method

Future fileSaveAs(
  1. List<int> bytes,
  2. String filepath
)

Implementation

static Future<dynamic> fileSaveAs(List<int> bytes, String filepath) async {
  // make the file name safe
  filepath = Mime.toSafeFileName(filepath);

  String? folder;
  try {
    Directory? directory = await getDownloadsDirectory();
    if (directory != null) folder = directory.path;
  } catch (e) {
    Log().debug('$e');
  }

  try {
    if (folder == null) {
      Directory? directory = await getTemporaryDirectory();
      folder = directory.path;
    }
  } catch (e) {
    Log().debug('$e');
  }

  try {
    if (folder == null) {
      Directory? directory = await getExternalStorageDirectory();
      if (directory != null) folder = directory.path;
    }
  } catch (e) {
    Log().debug('$e');
  }

  if (folder != null) {
    String path = join(folder, filepath);
    File file = File(path);
    await file.writeAsBytes(bytes);
    OpenFilex.open(path);
    return file;
  } else {
    System.toast("Unable to save file");
  }
}