Javascript 电子-将文件下载到特定位置
我需要将文件下载到电子程序中的特定位置。Javascript 电子-将文件下载到特定位置,javascript,download,directory,electron,downloadfile,Javascript,Download,Directory,Electron,Downloadfile,我需要将文件下载到电子程序中的特定位置。 我尝试实施,但失败。 然后我尝试实现,但没有意识到如何真正开始下载文件 如何将文件下载到特定位置,例如C:\Folder? 谢谢 正如你提到的那样,电子dl似乎是一种流行的方法。主要来自github页面:NPMI-S electron dl const {BrowserWindow} = require('electron'); const {download} = require('electron-dl'); download(BrowserWind
我尝试实施,但失败。
然后我尝试实现,但没有意识到如何真正开始下载文件 如何将文件下载到特定位置,例如
C:\Folder
?谢谢 正如你提到的那样,电子dl似乎是一种流行的方法。主要来自github页面:
NPMI-S electron dl
const {BrowserWindow} = require('electron');
const {download} = require('electron-dl');
download(BrowserWindow.getFocusedWindow(), "http://url-to-asset", {directory:"c:/Folder"})
我最终使用了。要发送下载请求(从
renderer.js
),请执行以下操作:
在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
中:
ipcRenderer.send("download", {
url: "URL is here",
properties: {directory: "Directory is here"}
});
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()));
});
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
});
在renderer.js
中:
ipcRenderer.send("download", {
url: "URL is here",
properties: {directory: "Directory is here"}
});
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()));
});
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应用程序中下载文件,您需要执行以下操作:
session
对象上发出的类似事件项
包含必要的事件(下载、失败、暂停等)和必要的方法(保存文件的位置)等如何将文件下载到C:/folder
您有两种选择:
如果希望为所有文件设置默认下载位置,则可以在会话对象上使用。然后这将是该会话的默认路径。问题是,我必须从渲染器页面调用API,所以我必须传递消息和类似的内容。此外,我必须能够跟踪下载进度,以便更新
元素,最后-我必须有一个事件,该事件将在下载完成时触发。当我调用.then()
时,Electron抛出了一个异常,因为“.then()未定义”。我实现了这段代码,但我需要给出下载文件所需的本地计算机文件夹路径。例如“\\192.168.XX.XXX\\D001\\P001\\sample.pdf”。它给了我错误“无法读取属性'get-typeof null”。请您帮助!失败是什么意思?使用代码示例将有助于解决问题。您是否在html中使用下载标记。从您使用该官方API主进程或渲染进程的位置根据我的理解,例如使用ipcMain
,我将一条消息从renderer.js
传递到main.js
,然后,当收到消息时-立即尝试开始将特定文件下载到发送位置-但未发生任何情况,即尝试失败。如何加快下载速度!例如,假设我想下载一个文件。如果你想像chrome一样下载它,它会变得很慢,毫无用处。我希望它像idm一样工作。以及如何暂停和恢复。