本文介紹了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 - 將文件下載到特定位置的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!