简体   繁体   English

电子 - 将文件下载到特定位置

[英]Electron - Download a file to a specific location

I need to download a file to a specific location in my Electron program. 我需要将文件下载到Electron程序中的特定位置。
I tried implementing this API but failed. 我尝试实现此API但失败了。
Then I tried implementing the official API , but couldn't realize how to actually start downloading the file. 然后我尝试实现官方API ,但无法实现如何实际开始下载文件。

How can I download a file to a specific location, say C:\\Folder ? 如何将文件下载到特定位置,例如C:\\Folder
Thanks! 谢谢!

I ended up using electron-dl . 我最终使用了电子dl
To send a download request (from the renderer.js ): 要发送下载请求(来自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
});

as you mentioned yourself, electron-dl seems to be the popular way to do that. 正如你自己提到的那样, electron-dl似乎是流行的方式。 Mainly from the github page: npm i -S 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"})

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-download which is emitted on Session object when the user clicks on a link to download a file and the file is going to be downloaded. 获得会话对象的实例后,您可以监听当用户单击链接下载文件并将要下载文件时在Session对象上发出的will-download等事件。

  3. The will-download event returns the item which is going to be downloaded. will-download事件返回的item ,其将被下载。 This item contains the necessary events (downloaded, failed, paused etc.) and necessary methods (where to save the file) etc. 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 item object, that you get from the event will-download . 您可以使用item对象设置文件的下载位置,您可以从事件中will-downloadwill-download Use the method setSavePath on the item object. 项目对象上使用方法setSavePath

If you rather want to set the default download location for all the files, then you can use, setDownloadPath on the session object. 如果您想要为所有文件设置默认下载位置,则可以在会话对象上使用setDownloadPath Then that will be the default path for that session. 那么这将是该会话的默认路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM