I am new to Flutter and working in a flutter web application, My requirement is to create and download a text file. like below.
void getData() {
List<int> bytes = utf8.encode('this is the text file');
print(bytes); // Need to download this with txt file.
}
Can anyone help me to achieve this
First, update your ProjectFolder/web/index. html file to include the library and define the webSaveAs function like so: ... <script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.min.js"> </script> <script> function webSaveAs(blob, name) { saveAs(blob, name); } </script> <script src="main.
How to Download File From URL and Save to Phone Storage In Flutter? User can try with the available plugin flutter_downloader like below: savedDir: 'the path of directory where you want to save downloaded files', showNotification: true, // show download progress in status bar (for Android)
Flutter has a built in type called File which will allow us to read and write to a file locally. To make our lives easier we do need to add a package, the path_provider package, which you can find here. The path_provider package is used for "finding commonly used locations on the filesystem".
The path_provider package is used for "finding commonly used locations on the filesystem". Before we get started, be sure to add the following to your pubspec.yaml under dependencies: Once you have added that to your pubspec, you can run flutter pub get, after that has completed remember to import the following where ever it is required:
Create the full file path as a String. Now that we have the getFilePath function written, we can use that to create a new instance of File and then use that to write some text to the file. To do this we need to add the following saveFile function:
This method is based on manipulations with an HTML
document.
Some additional packages should be imported:
import 'dart:convert';
import 'dart:html' as html; // or package:universal_html/prefer_universal/html.dart
Code snippet:
final text = 'this is the text file';
// prepare
final bytes = utf8.encode(text);
final blob = html.Blob([bytes]);
final url = html.Url.createObjectUrlFromBlob(blob);
final anchor = html.document.createElement('a') as html.AnchorElement
..href = url
..style.display = 'none'
..download = 'some_name.txt';
html.document.body.children.add(anchor);
// download
anchor.click();
// cleanup
html.document.body.children.remove(anchor);
html.Url.revokeObjectUrl(url);
Here is DartPad
demo.
Got another way to do it, via popular JS library called FileSaver
First, update your ProjectFolder/web/index.html
file to include the library and define the webSaveAs
function like so:
...
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.min.js">
</script>
<script>
function webSaveAs(blob, name) {
saveAs(blob, name);
}
</script>
<script src="main.dart.js" type="application/javascript"></script>
...
Then you can call this function from Dart code like so:
import 'dart:js' as js;
import 'dart:html' as html;
...
js.context.callMethod("webSaveAs", [html.Blob([bytes]), "test.txt"])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With