Electron - Download a file to a specific location(Electron - 将文件下载到特定位置)
问题描述
我需要将文件下载到我的 Electron 程序中的特定位置.
我尝试实现 此 API 但失败了.
然后我尝试实现官方API,但不知道如何实际启动下载文件.
I need to download a file to a specific location in my Electron program.
I tried implementing this API but failed.
Then I tried implementing the official API, but couldn't realize how to actually start downloading the file.
如何将文件下载到特定位置,例如 C:Folder
?
谢谢!
How can I download a file to a specific location, say C:Folder
?
Thanks!
推荐答案
我最终使用了 electron-dl.
发送下载请求(来自 renderer.js
):
ipcRenderer.send("download", {
url: "URL is here",
properties: {directory: "Directory is here"}
});
在 main.js
中,您的代码如下所示:
In the main.js
, your code would look something like this:
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
中,如下所示:
The "download complete" listener is in the renderer.js
, and would look like:
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
});
这篇关于Electron - 将文件下载到特定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Electron - 将文件下载到特定位置


- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01