fileSaveAs static method

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

Implementation

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

    final blob = Blob([bytes]);
    final url = Url.createObjectUrlFromBlob(blob);
    final anchor = document.createElement('a') as AnchorElement;

    anchor.href = url;
    anchor.style.display = 'none';
    anchor.download = filename;
    document.body!.children.add(anchor);

    anchor.click();

    document.body!.children.remove(anchor);
    Url.revokeObjectUrl(url);
  } catch (e) {
    System.toast("Unable to save file");
    Log().error('Error writing file');
    Log().exception(e);
    return null;
  }

  return null;
}