Flutter WEB download option

前端 未结 6 1789
萌比男神i
萌比男神i 2021-01-01 15:43

I am making flutter web app that should generate a file from user data. And have the option to download the output file.

But I can not find any options/packages whic

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-01 16:00

    One way to trigger download is the adapt a common pattern used in "native" javascript, create an anchor element with the download attribute and trigger a click.

    import 'dart:convert';
    import 'dart:html';
    
    main() {
      File file = // generated somewhere
      final rawData = file.readAsBytesSync();
      final content = base64Encode(rawData);
      final anchor = AnchorElement(
          href: "data:application/octet-stream;charset=utf-16le;base64,$content")
        ..setAttribute("download", "file.txt")
        ..click();
    }
    
    

提交回复
热议问题