Skip to content

How can I downlaod a file with this package? #177

Open
@JgomesAT

Description

@JgomesAT

With dart:html i used this to download a file,

`static void _downloadFile(List bytes, String fileNameWithExtension) {
final html.Blob blob = html.Blob([bytes]);
final String url = html.Url.createObjectUrlFromBlob(blob);
final html.AnchorElement anchor = html.document.createElement('a') as html.AnchorElement
..href = url
..style.display = 'none'
..download = fileNameWithExtension;
html.document.body!.children.add(anchor);

// download
anchor.click();

// cleanup
html.document.body!.children.remove(anchor);
html.Url.revokeObjectUrl(url);

}`
and work fine

now I try to refactor to use the new package web

`static void _downloadFile(List bytes, String fileNameWithExtension) {
final web.Blob blob = web.Blob(bytes.map((int byte) => byte.toJS).toList().toJS);
final String url = web.URL.createObjectURL(blob.toJSBox);
final web.HTMLAnchorElement anchor = web.document.createElement('a') as web.HTMLAnchorElement
..href = url
..style.display = 'none'
..download = fileNameWithExtension;
// Insert new elements in the DOM:
web.document.body!.appendChild(anchor);

// download
anchor.click();

// cleanup
web.document.body!.removeChild(anchor);
web.URL.revokeObjectURL(url);

}`

but it doesn't work

Error: Attempting to box non-Dart object.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-questionA question about expected behavior or functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions