Skip to content

Instantly share code, notes, and snippets.

@simolus3
Created July 12, 2019 07:31
Show Gist options
  • Save simolus3/05493667533cc7841ecda7626e749b51 to your computer and use it in GitHub Desktop.
Save simolus3/05493667533cc7841ecda7626e749b51 to your computer and use it in GitHub Desktop.
Download file in dart
import 'dart:convert';
import 'dart:html';
import 'dart:typed_data';
void main() {
final content = '["general kenobi", "you are a bold one"]';
download('message.json', utf8.encode(content) as Uint8List, type: 'application/json');
}
void download(String filename, Uint8List data, {String type = 'octet/stream'}) {
final blob = Blob([data], type);
final url = Url.createObjectUrlFromBlob(blob);
final a = AnchorElement()
..style.display = 'none'
..href = url
..download = filename;
document.body.append(a);
a.click();
Url.revokeObjectUrl(url);
a.remove();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment