web-dev-qa-db-fra.com

Option de téléchargement Flutter WEB

Je crée une application Web Flutter qui devrait générer un fichier à partir des données utilisateur. Et avez la possibilité de télécharger le fichier de sortie.

Mais je ne trouve aucune option/package qui fonctionne pour Flutter Web :(

Quelqu'un peut-il m'aider s'il vous plaît?

14
Pratik

vous pouvez utiliser le package url_launcher avec rl_launcher_web

alors vous pouvez faire:

launch("data:application/octet-stream;base64,${base64Encode(yourFileBytes)}")

EDIT: vous n'avez pas besoin d'un plugin si vous faites cela

download.Dart:

import 'Dart:convert';
// ignore: avoid_web_libraries_in_flutter
import 'Dart:html';

void download(
  List<int> bytes, {
  String downloadName,
}) {
  // Encode our file in base64
  final _base64 = base64Encode(bytes);
  // Create the link with the file
  final anchor =
      AnchorElement(href: 'data:application/octet-stream;base64,$_base64')
        ..target = 'blank';
  // add the name
  if (downloadName != null) {
    anchor.download = downloadName;
  }
  // trigger download
  document.body.append(anchor);
  anchor.click();
  anchor.remove();
  return;
}

empty_download.Dart:

void download(
  List<int> bytes, {
  String downloadName,
}) {
  print('I do nothing');
}

importer et utiliser:

import 'empty_download.Dart'
if (Dart.library.html) 'download.Dart';

void main () {
  download('I am a test file'.codeUnits, // takes bytes
      downloadName: 'test.txt');
}
0
pinguinitorawr