Created
July 12, 2019 07:31
-
-
Save simolus3/05493667533cc7841ecda7626e749b51 to your computer and use it in GitHub Desktop.
Download file in dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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