【问题标题】:Can't download xml file from local filesystem无法从本地文件系统下载 xml 文件
【发布时间】:2021-12-23 04:23:42
【问题描述】:

我正在开发一个电子应用程序,并且我有一个锚点,它应该下载一个 xml 文件,该文件位于用户计算机的 temp 文件夹中。当我点击它时,会出现下载弹出窗口,我可以选择保存文件的位置,但是当我按下保存时,文件不会保存在指定的文件夹中。在开发工具的网络选项卡上没有任何显示。

这是我的代码

<a
  :href="filePath"
  class="custom-button-primary big px-3 py-2"
  download="saft.xml"
  style="text-decoration: none;"
  >Download file</a>

【问题讨论】:

    标签: html vuejs2 electron


    【解决方案1】:

    我找到了解决方案。至少在 Electron 中,似乎无法通过锚点访问本地系统上的文件。因此,为了保存,您需要使用fs 模块实现保存机制

      import { dialog } from 'electron';
      import { copyFile } from "fs/promises";
      const downloadFile = (filePath) => {
      dialog
        .showSaveDialog({
          title: "Your title",
          defaultPath: "Default path / Filename",
          properties: ["showOverwriteConfirmation"],
        })
        .then(async (result) => {
          await copyFile(filePath, result.filePath);
        })
        .catch((err) => {
          alert(err);
        });
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      • 1970-01-01
      相关资源
      最近更新 更多