electron-dl模块设置文件保存目录
electron-dl模块设置文件保存目录要设置electron-dl模块的文件保存目录,可以使用 options 参数来指定保存路径。以下是示例代码:
const { app, BrowserWindow } = require('electron');
const { download } = require('electron-dl');
const path = require('path');
let mainWindow;
function createWindow() {
mainWindow = new BrowserWindow();
mainWindow.loadURL('https://example.com');
mainWindow.webContents.on('did-finish-load', () => {
const options = {
directory: path.join(app.getPath('downloads'), 'my-folder'), // 设置保存目录
};
download(mainWindow, 'https://example.com/file.ext', options)
.then(dl => {
console.log(`文件已保存至: ${dl.getSavePath()}`);
})
.catch(error => {
console.error('文件下载失败:', error);
});
});
}
app.on('ready', () => {
createWindow();
});
在上述示例中,我们使用 options 参数来设置保存目录。 directory 选项指定了保存文件的目录路径。在这个示例中,我们使用 app.getPath('downloads') 来获取用户下载目录的路径,并将其与自定义的文件夹名 my-folder 拼接起来作为保存目录。
请注意,示例中的 https://example.com/file.ext 需要替换为实际的下载文件的URL。确保已在项目中安装了electron-dl模块和path模块。
这样,electron-dl模块将会将下载的文件保存到指定的目录中。
页:
[1]