【问题标题】:Electron - Download a file to a specific locationElectron - 将文件下载到特定位置
【发布时间】:2018-02-16 13:49:08
【问题描述】:

我需要将文件下载到我的 Electron 程序中的特定位置。
我尝试实现 this API 但失败了。
然后我尝试实现the official API,但无法意识到如何真正开始下载文件。

如何将文件下载到特定位置,例如C:\Folder
谢谢!

【问题讨论】:

  • 失败是什么意思。Saring 代码示例将有助于找出问题。您是否在 html 中使用了下载标签。从您使用该官方 API 主进程或渲染进程的位置
  • 据我了解,作为示例使用ipcMain,我将一条消息从renderer.js 传递给main.js,然后在收到消息时-立即尝试开始将特定文件下载到发送的位置 - 除了没有发生任何事情,即尝试失败。

标签: javascript download directory electron downloadfile


【解决方案1】:

我最终使用了electron-dl
发送下载请求(来自renderer.js):

ipcRenderer.send("download", {
    url: "URL is here",
    properties: {directory: "Directory is here"}
});

main.js 中,您的代码如下所示:

const {app, BrowserWindow, ipcMain} = require("electron");
const {download} = require("electron-dl");
let window;
app.on("ready", () => {
    window = new BrowserWindow({
        width: someWidth,
        height: someHeight
    });
    window.loadURL(`file://${__dirname}/index.html`);
    ipcMain.on("download", (event, info) => {
        download(BrowserWindow.getFocusedWindow(), info.url, info.properties)
            .then(dl => window.webContents.send("download complete", dl.getSavePath()));
    });
});

“下载完成”侦听器位于renderer.js,如下所示:

const {ipcRenderer} = require("electron");
ipcRenderer.on("download complete", (event, file) => {
    console.log(file); // Full file path
});

如果您想跟踪下载进度:

main.js:

ipcMain.on("download", (event, info) => {
    info.properties.onProgress = status => window.webContents.send("download progress", status);
    download(BrowserWindow.getFocusedWindow(), info.url, info.properties)
        .then(dl => window.webContents.send("download complete", dl.getSavePath()));
});

renderer.js:

ipcRenderer.on("download progress", (event, progress) => {
    console.log(progress); // Progress in fraction, between 0 and 1
    const progressInPercentages = progress * 100; // With decimal point and a bunch of numbers
    const cleanProgressInPercentages = Math.floor(progress * 100); // Without decimal point
});

【讨论】:

  • 如何加快下载速度!例如,假设我们想下载一个文件。如果要像chrome一样下载它,速度很慢,它就变得没用了。我希望它像 idm 一样工作。以及如何暂停和恢复。
【解决方案2】:

正如您自己提到的,electron-dl 似乎是实现此目的的流行方式。主要来自github页面:npm i -S electron-dl

const {BrowserWindow} = require('electron');
const {download} = require('electron-dl');
download(BrowserWindow.getFocusedWindow(), "http://url-to-asset", {directory:"c:/Folder"})

【讨论】:

  • 问题是,我必须从渲染器页面调用 API,所以我必须传递消息和类似的东西。此外,我必须能够跟踪下载进度,以便更新<progress> 元素,最后 - 我必须有一个在下载完成时触发的事件。当我调用.then() 时,Electron 抛出异常,因为“.then() 未定义”。
  • 我实现了这段代码,但我需要提供我需要从中下载文件的本地计算机文件夹路径。例如“\\192.168.XX.XXX\\D001\\P001\\sample.pdf”。它给了我错误“无法读取属性'get typeof null”。
  • 你能帮忙吗!
  • 你好。我也面临同样的问题,谢谢你。在多个文件上,进度条考虑 100% 下载小的正 cmets。我该如何度过这个难关?
【解决方案3】:

要允许用户在 Electron 应用程序中下载文件,您需要执行以下操作:

  1. 从分区中获取默认会话或用户会话。见Session

  2. 一旦你有了会话对象的实例,你就可以监听像will-download这样的事件,当用户点击一个链接下载文件并且文件正在运行时在Session对象上发出待下载。

  3. will-download 事件返回要下载的item。这个item 包含必要的事件(下载、失败、暂停等)和必要的方法(保存文件的位置)等。

现在,关于How to download a file to C:/folder 上的查询?

您有两个选择:

  1. 您可以要求用户设置下载位置(默认行为)
  2. 您可以使用从事件will-download 获得的item 对象设置文件的下载位置。在item 对象上使用方法setSavePath

如果您想为所有文件设置默认下载位置,则可以在会话对象上使用setDownloadPath。那么这将是该会话的默认路径。

【讨论】:

    猜你喜欢
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 2022-10-19
    相关资源
    最近更新 更多