Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save and download text file in Flutter web application

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

like image 305
Chinnu Avatar asked Jan 09 '20 11:01

Chinnu


People also ask

How do you save in web flutter?

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?

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)

How to read and write to a file in flutter?

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".

How do I use the path_provider package in flutter?

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:

How to write text to a file using getfilepath function?

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:


2 Answers

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.

like image 155
Spatz Avatar answered Oct 14 '22 00:10

Spatz


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"])
like image 29
Oleksii Shliama Avatar answered Oct 14 '22 00:10

Oleksii Shliama