【问题标题】:Download TXT file from URL and save to PC on Flutter Web从 URL 下载 TXT 文件并保存到 Flutter Web 上的 PC
【发布时间】:2021-05-17 01:40:04
【问题描述】:

我的项目有问题,我想下载一个 TXT 文件并使用 Flutter WEB 保存到任何目录,我想我设置了但它不起作用。现在 de txt 文件在浏览器的同一选项卡中显示内容,但我想下载这个文件,这是我的代码:

void downloadFile(String url) {
    html.AnchorElement anchorElement = new html.AnchorElement(href: url);
    anchorElement.download = "plantilla_simulador.txt";
    anchorElement.dispatchEvent(html.Event.eventType('MouseEvent', 'click'));
    anchorElement.style.display = 'none';
    anchorElement.click();
  }

有人有这样做的想法吗?非常感谢。

【问题讨论】:

    标签: flutter-web downloadfile


    【解决方案1】:

    也许您正试图从其他站点下载文件。 请记住,download attribute 仅适用于 same-origin URLs,或 blob:data: 方案。

    此代码应下载文件:

    html.AnchorElement(href: 'index.html')
      ..download = 'some_name.txt'
      ..style.display = 'none'
      ..click();
    

    但是这个应该在浏览器中打开文件:

    html.AnchorElement(href: 'https://raw.githubusercontent.com/flutter/flutter/master/flutter_console.bat')
      ..download = 'some_name.txt'
      ..style.display = 'none'
      ..click();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-07
      • 1970-01-01
      • 2021-12-16
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      • 2015-12-08
      • 2019-10-31
      相关资源
      最近更新 更多