javascript Electron - 将文件下载到特定位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46102851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 06:27:48  来源:igfitidea点击:

Electron - Download a file to a specific location

javascriptdownloaddirectoryelectrondownloadfile

提问by avi12

I need to download a file to a specific location in my Electron program.
I tried implementing this APIbut failed.
Then I tried implementing the official API, but couldn't realize how to actually start downloading the file.

我需要将文件下载到 Electron 程序中的特定位置。
我尝试实现这个 API但失败了。
然后我尝试实现官方 API,但无法意识到如何实际开始下载文件。

How can I download a file to a specific location, say C:\Folder?
Thanks!

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

回答by avi12

I ended up using electron-dl.
To send a download request (from the renderer.js):

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

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

In the main.js, your code would look something like this:

在 中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()));
    });
});

The "download complete" listener is in the renderer.js, and would look like:

“下载完成”侦听器位于 中renderer.js,看起来像:

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

If you want to track your download's progress:

如果您想跟踪下载进度:

In main.js:

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()));
});

In renderer.js:

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
});

回答by user2520818

as you mentioned yourself, electron-dlseems to be the popular way to do that. Mainly from the github page: npm i -S electron-dl

正如您自己提到的,电子 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"})

回答by Akshay Anurag

To allow a user to download a file in an Electron application, you need to do the following:

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

  1. Get either the default session or the session of the user from the partition. See Session

  2. Once you have an instance of the session object, you can then listen for events like will-downloadwhich is emitted on Sessionobject when the user clicks on a link to download a file and the file is going to be downloaded.

  3. The will-downloadevent returns the itemwhich is going to be downloaded. This itemcontains the necessary events (downloaded, failed, paused etc.) and necessary methods (where to save the file) etc.

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

  2. 一旦您拥有会话对象的实例,您就可以监听诸如当用户单击链接下载文件并且文件将被下载时will-downloadSession对象上发出的事件。

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

Now, regarding the query on How to download a file to C:/folder?

现在,关于How to download a file to C:/folder?

You have 2 choices regarding that:

您有 2 个选择:

  1. You can either ask the user to set the download location (Default behavior)
  2. You can set the download location for the file using itemobject, that you get from the event will-download. Use the method setSavePathon the itemobject.
  1. 您可以要求用户设置下载位置(默认行为)
  2. 您可以使用item从 event 中获取的 object设置文件的下载位置will-downloadsetSavePathitem对象上 使用该方法。

If you rather want to set the default download location for all the files, then you can use, setDownloadPathon the session object. Then that will be the default path for that session.

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