【问题标题】:Flutter Web Page to download a file from a serverFlutter Web Page 从服务器下载文件
【发布时间】:2026-01-08 18:15:02
【问题描述】:

我的一个学校项目的目标是用 Flutter 制作一个网站。该站点应将多个文件显示为一个列表,其中每个文件都有一个下载按钮。到目前为止一切都很简单。

我的问题是我无法下载存储在本地或通过下载 URL 的文件。我尝试使用 dart HTML 插件和 dio 包来让它工作。没有运气。

所以我的问题是:有没有办法让“onPressed”事件下载本地文件或通过下载 URL?

提前致谢

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    你可以使用this plugin从flutter下载文件

    使用起来非常简单。我正在添加一个 sn-p 代码以集成到您的 webview(导航到下载 URL 时的 naviagtionDelegate)

    final taskId = await FlutterDownloader.enqueue(
      url: 'your download link',
      savedDir: 'the path of directory where you want to save downloaded files',
      showNotification: true, // show download progress in status bar (for Android)
      openFileFromNotification: true, // click on notification to open downloaded file (for Android)
    );
    

    【讨论】:

    • 感谢您的建议。虽然我正在努力让它发挥作用。目前这是我的代码的外观:void downloadFile() async { WidgetsFlutterBinding.ensureInitialized(); await FlutterDownloader.initialize(debug: true); final taskId = await FlutterDownloader.enqueue(url:"https://groups.csail.mit.edu/graphics/classes/6.837/F03/models/teapot.obj",savedDir: "path to my downloads folder"); } 这会引发 UnimplementedError
    • 能否将代码粘贴到 pastebin 以便我解决问题
    • pastebin.com/zSff17Mv就是下载方式。
    【解决方案2】:

    你可以使用包Url Launcher

    安装后,您可以: onPressed: () => 启动('https://www.google.com/file.pdf')

    【讨论】:

    • 感谢您的建议。我也许应该指定我不是在制作移动应用程序。因此,当使用您的代码时,它所做的只是在我的浏览器中使用文件 URL 打开一个新选项卡。