提交 96fba9a7 authored 作者: 蔡芬's avatar 蔡芬

文件打开

上级 fdda23a4
...@@ -4,7 +4,7 @@ import { app, protocol, BrowserWindow, shell, Menu, ipcMain, globalShortcut } fr ...@@ -4,7 +4,7 @@ import { app, protocol, BrowserWindow, shell, Menu, ipcMain, globalShortcut } fr
import path from 'path'; import path from 'path';
import { createProtocol } from "vue-cli-plugin-electron-builder/lib"; import { createProtocol } from "vue-cli-plugin-electron-builder/lib";
import installExtension, { VUEJS_DEVTOOLS } from "electron-devtools-installer"; import installExtension, { VUEJS_DEVTOOLS } from "electron-devtools-installer";
import { downloadFile } from '@/libs/utils/backfunction.js'; import { downloadFile, openFile, openFileFolder, getFilePositionFromUrl } from '@/libs/utils/backfunction.js';
const isDevelopment = process.env.NODE_ENV !== "production"; const isDevelopment = process.env.NODE_ENV !== "production";
// Scheme must be registered before the app is ready // Scheme must be registered before the app is ready
...@@ -130,13 +130,18 @@ async function createWindow() { ...@@ -130,13 +130,18 @@ async function createWindow() {
console.log('主进程监听到下载',downLoadUrl,targetUrl); console.log('主进程监听到下载',downLoadUrl,targetUrl);
downloadFile(downLoadUrl, targetUrl,({percentage,received,total,success})=>{ downloadFile(downLoadUrl, targetUrl,({percentage,received,total,success,filepath, filePosition})=>{
if(!!success) { if(!!success) {
console.log('文件下载成功'); console.log('文件下载成功');
// 下载完成打开文件
let openStatus = openFile(filepath); //同浏览器效果 - 打开文件
// let openStatus = openFileFolder(filepath);
// console.log('打开文件位置状态',openStatus,filepath)
return; return;
} }
console.log(percentage + "% | " + received + " bytes out of " + total + " bytes."); // console.log(percentage + "% | " + received + " bytes out of " + total + " bytes.");
}); });
// electron 的下载一直 interrupted -- 失败 // electron 的下载一直 interrupted -- 失败
// downloadpath = downLoadUrl; // downloadpath = downLoadUrl;
// folderpath = targetUrl; // folderpath = targetUrl;
......
import { shell } from 'electron';
var request = require('request'); var request = require('request');
var fs = require('fs'); var fs = require('fs');
// 下载文件
export const downloadFile = function(file_url , targetPath, callback) { export const downloadFile = function(file_url , targetPath, callback) {
// Save variable to know progress // Save variable to know progress
var received_bytes = 0; var received_bytes = 0;
...@@ -14,7 +18,6 @@ export const downloadFile = function(file_url , targetPath, callback) { ...@@ -14,7 +18,6 @@ export const downloadFile = function(file_url , targetPath, callback) {
req.pipe(out); req.pipe(out);
req.on('response', function ( data ) { req.on('response', function ( data ) {
console.log('Response',data)
// Change the total bytes value to get progress later. // Change the total bytes value to get progress later.
total_bytes = parseInt(data.headers['content-length' ]); total_bytes = parseInt(data.headers['content-length' ]);
}); });
...@@ -28,7 +31,7 @@ export const downloadFile = function(file_url , targetPath, callback) { ...@@ -28,7 +31,7 @@ export const downloadFile = function(file_url , targetPath, callback) {
req.on('end', function() { req.on('end', function() {
console.log("File succesfully downloaded"); console.log("File succesfully downloaded");
callback({success:true}); callback({success:true, filepath: targetPath, filePosition: getFilePositionFromUrl(targetPath) });
}); });
} }
export const showProgress = function(received, total, callback){ export const showProgress = function(received, total, callback){
...@@ -36,3 +39,29 @@ export const showProgress = function(received, total, callback){ ...@@ -36,3 +39,29 @@ export const showProgress = function(received, total, callback){
// console.log(percentage + "% | " + received + " bytes out of " + total + " bytes."); // console.log(percentage + "% | " + received + " bytes out of " + total + " bytes.");
callback({percentage,received,total}); callback({percentage,received,total});
} }
// 打开文件
export const openFile = function(path) {
if(!fs.existsSync(path)) return false;
shell.openPath(path);
return true;
}
// 打开文件所在位置
export const openFileFolder = function(fullPath) {
if(!fs.existsSync(fullPath)) return false;
shell.showItemInFolder(fullPath); //当前没有效果 ???
return true;
}
// 提取文件名
export const getFilenameFromUrl = function(fileUrl) {
return fileUrl.substring(fileUrl.lastIndexOf('/') + 1);
}
// 提取文件位置
export const getFilePositionFromUrl = function(fileUrl) {
return fileUrl.substring(0, fileUrl.lastIndexOf('/'));
}
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论