downloadFile static method

Future<bool> downloadFile(
  1. FileModel fileModel
)

Implementation

static Future<bool> downloadFile(FileModel fileModel) async {
  bool success = false;

  try {
    String url = URL.createObjectURL(
      Blob(
        <JSUint8Array>[fileModel.bytes.toJS].toJS,
        BlobPropertyBag(type: fileModel.mimeType),
      ),
    );

    Document htmlDocument = document;
    HTMLAnchorElement anchor =
        htmlDocument.createElement('a') as HTMLAnchorElement;
    anchor.href = url;
    anchor.style.display = fileModel.name + fileModel.ext;
    anchor.download = fileModel.name + fileModel.ext;
    document.body!.add(anchor);
    anchor.click();
    anchor.remove();
    success = true;
  } catch (e) {
    rethrow;
  }
  return success;
}