您的位置:首页 > 娱乐 > 明星 > electron项目中实现视频下载保存到本地

electron项目中实现视频下载保存到本地

2024/10/6 5:59:19 来源:https://blog.csdn.net/DLGDark/article/details/140514136  浏览:    关键词:electron项目中实现视频下载保存到本地
  • 第一种方式:用户自定义选择下载地址位置

渲染进程

// 渲染进程// 引入
import { ipcRenderer } from "electron";// 列表行数据下载视频操作,diffVideoUrl 是视频请求地址
handleDownloadClick(row) {if (!row.diffVideoUrl) {this.$message.error("暂无视频,请稍后重试下载");} else {//渲染线程主动发送 downloadVideo事件到主线程请求下载视频ipcRenderer.send("downloadVideo", row.diffVideoUrl, row.orderCode);}
}

主进程

// 主进程// 引入
import { ipcMain, dialog } from "electron";
import path from "path";
import fs from "fs";
import axios from "axios";// 监听渲染进程下载视频
ipcMain.on("downloadVideo", async (event, videoUrl, fileName) => {let result = await dialog.showOpenDialog({properties: ["openDirectory", "createDirectory", "promptToCreate"],});if (!result.canceled) {// 用户选择的路径let directoryPath = result.filePaths[0];// 获取目标文件的路径const destPath = path.join(directoryPath, fileName + ".mp4");try {// 请求七牛视频地址接口,获取视频const response = await axios({method: "get",url: videoUrl,responseType: "stream", // 以流的形式获取响应体,用于写入文件});// 在用户选择的目标文件路径下创建一个可写流const ws = fs.createWriteStream(destPath);// 将数据流保存到文件中response.data.pipe(ws);dialog.showMessageBox(mainWindow, {message: "已下载成功!",type: "none",});} catch (error) {console.log(error);dialog.showMessageBox(mainWindow, {message: "下载失败!",type: "none",});}}
});
  • 第二种方式:系统内部设置默认下载地址位置

渲染进程

// 渲染进程// 引入
import { ipcRenderer } from "electron";// 列表行数据下载视频操作,diffVideoUrl 是视频请求地址
handleDownloadClick(row) {if (!row.diffVideoUrl) {this.$message.error("暂无视频,请稍后重试下载");} else {//渲染线程主动发送 downloadVideo事件到主线程请求下载视频ipcRenderer.send("downloadVideo", row.diffVideoUrl, row.orderCode);}
}

主进程

// 主进程// 引入
import { app, ipcMain, dialog } from "electron";
import path from "path";
import fs from "fs";
import axios from "axios";// 监听渲染进程下载视频
ipcMain.on("downloadVideo", async (event, videoUrl, fileName) => {// 默认下载到电脑 downloads 目录下let directoryPath = app.getPath("downloads");// 获取目标文件的路径const destPath = path.join(directoryPath, fileName + ".mp4");try {// 请求七牛视频地址接口,获取视频const response = await axios({method: "get",url: videoUrl,responseType: "stream", // 以流的形式获取响应体,用于写入文件});// 在用户选择的目标文件路径下创建一个可写流const ws = fs.createWriteStream(destPath);// 将数据流保存到文件中response.data.pipe(ws);dialog.showMessageBox(mainWindow, {message: "已下载成功!",type: "none",});} catch (error) {console.log(error);dialog.showMessageBox(mainWindow, {message: "下载失败!",type: "none",});}
});

代码中相关代码点解释:

mainWindownew BrowserWindow() 创建应用程序窗口,此处省略相关代码,例子代码如下:

// 在主进程中.
const { BrowserWindow } = require('electron')const mainWindow = new BrowserWindow({ width: 800, height: 600 })

dialog.showOpenDialog 参考官方文档:dialog.showOpenDialog

dialog.showOpenDialog
app.getPath 参考官方文档:app.getPath

app.getPath
如有其它问题,请在评论区留言,博主看到都会回复的~

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com