electron項目中實現(xiàn)視頻下載保存到本地的方式
第一種方式:用戶自定義選擇下載地址位置
渲染進程
// 渲染進程
// 引入
import { ipcRenderer } from "electron";
// 列表行數(shù)據(jù)下載視頻操作,diffVideoUrl 是視頻請求地址
handleDownloadClick(row) {
if (!row.diffVideoUrl) {
this.$message.error("暫無視頻,請稍后重試下載");
} else {
//渲染線程主動發(fā)送 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";
// 監(jiān)聽渲染進程下載視頻
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", // 以流的形式獲取響應(yīng)體,用于寫入文件
});
// 在用戶選擇的目標文件路徑下創(chuàng)建一個可寫流
const ws = fs.createWriteStream(destPath);
// 將數(shù)據(jù)流保存到文件中
response.data.pipe(ws);
dialog.showMessageBox(mainWindow, {
message: "已下載成功!",
type: "none",
});
} catch (error) {
console.log(error);
dialog.showMessageBox(mainWindow, {
message: "下載失?。?,
type: "none",
});
}
}
});第二種方式:系統(tǒng)內(nèi)部設(shè)置默認下載地址位置
渲染進程
// 渲染進程
// 引入
import { ipcRenderer } from "electron";
// 列表行數(shù)據(jù)下載視頻操作,diffVideoUrl 是視頻請求地址
handleDownloadClick(row) {
if (!row.diffVideoUrl) {
this.$message.error("暫無視頻,請稍后重試下載");
} else {
//渲染線程主動發(fā)送 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";
// 監(jiān)聽渲染進程下載視頻
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", // 以流的形式獲取響應(yīng)體,用于寫入文件
});
// 在用戶選擇的目標文件路徑下創(chuàng)建一個可寫流
const ws = fs.createWriteStream(destPath);
// 將數(shù)據(jù)流保存到文件中
response.data.pipe(ws);
dialog.showMessageBox(mainWindow, {
message: "已下載成功!",
type: "none",
});
} catch (error) {
console.log(error);
dialog.showMessageBox(mainWindow, {
message: "下載失敗!",
type: "none",
});
}
});代碼中相關(guān)代碼點解釋:
mainWindow 是 new BrowserWindow() 創(chuàng)建應(yīng)用程序窗口,此處省略相關(guān)代碼,例子代碼如下:
// 在主進程中.
const { BrowserWindow } = require('electron')
const mainWindow = new BrowserWindow({ width: 800, height: 600 })dialog.showOpenDialog 參考官方文檔:dialog.showOpenDialog

app.getPath 參考官方文檔:app.getPath

到此這篇關(guān)于electron項目中實現(xiàn)視頻下載保存到本地的文章就介紹到這了,更多相關(guān)electron視頻下載保存到本地內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
BootStrap Validator 版本差異問題導(dǎo)致的submitHandler失效問題的解決方法
這篇文章主要介紹了BootStrap Validator 版本差異問題導(dǎo)致的submitHandler失效問題的解決方法,下面通過本文給大家詳細說明一下,需要的朋友可以參考下2016-12-12
JavaScript數(shù)組中reduce方法的應(yīng)用詳解
JavaScript 中的reduce()方法可以用于將數(shù)組元素匯總為單個值,,所以本文為大家整理了一些JavaScript數(shù)組中reduce方法的應(yīng)用,需要的可以參考一下2023-07-07
JavaScript使用正則表達式獲取全部分組內(nèi)容的方法示例
這篇文章主要介紹了JavaScript使用正則表達式獲取全部分組內(nèi)容的方法,結(jié)合實例形式分析了javascript正則匹配的相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
Js中FileReader讀取文件內(nèi)容方法詳解(async/await)
這篇文章主要給大家介紹了關(guān)于Js中FileReader讀取文件內(nèi)容(async/await)的相關(guān)資料,FileReader是前端進行文件處理的一個重要的Api,特別是在對圖片的處理上,如果你想知道圖片的處理原理,你就永遠不可能繞過它,需要的朋友可以參考下2023-11-11
JavaScript數(shù)組方法之findIndex()的用法詳解
findIndex()方法是一個非常實用的數(shù)組方法,可以幫助我們快速查找符合某個條件的元素,本文給大家介紹JavaScript數(shù)組方法之findIndex()的用法,感謝的朋友跟隨小編一起看看吧2023-10-10
JavaScript中的淺拷貝和深拷貝原理與實現(xiàn)淺析
這篇文章主要介紹了JavaScript中的淺拷貝和深拷貝原理與實現(xiàn),JavaScript 中的淺拷貝和深拷貝指的是在復(fù)制對象(包括對象、數(shù)組等)時,是否只復(fù)制對象的引用地址或者在復(fù)制時創(chuàng)建一個新的對象2023-04-04

