基础文件上传核心开发

This commit is contained in:
朱浩 2024-07-15 20:05:15 +08:00
parent 48501e9195
commit 3369f39374
4 changed files with 131 additions and 22 deletions

View File

@ -1,9 +1,9 @@
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
import { ElectronDownloadManager } from 'electron-dl-manager'; import { ElectronDownloadManager } from 'electron-dl-manager'
const manager = new ElectronDownloadManager(); import { dialog } from 'electron'
const manager = new ElectronDownloadManager()
export default async function ({ app, shell, BrowserWindow, ipcMain }) { export default async function ({ app, shell, BrowserWindow, ipcMain }) {
const userDataPath = app.getPath('userData') const userDataPath = app.getPath('userData')
//默认浏览器打开url //默认浏览器打开url
ipcMain.on('open-url-browser', (e, url) => { ipcMain.on('open-url-browser', (e, url) => {
@ -38,17 +38,65 @@ export default async function ({ app, shell, BrowserWindow, ipcMain }) {
id, id,
percentCompleted, percentCompleted,
// Get the number of bytes received so far // Get the number of bytes received so far
bytesReceived: item.getReceivedBytes(), bytesReceived: item.getReceivedBytes()
}); })
}, },
onDownloadCompleted: async ({ id, item }) => { onDownloadCompleted: async ({ id, item }) => {
console.log(item) console.log(item)
}, },
onDownloadCancelled: async () => {}, onDownloadCancelled: async () => {},
onDownloadInterrupted: async () => {}, onDownloadInterrupted: async () => {},
onError: (err, data) => {}, onError: (err, data) => {}
} }
}); })
})
})
/**...
* 接收渲染进程 保存文件的 的通知
* @param {Object} event
* @param {String} url 下载链接
* @param {String} fileName 文件名称包括后缀名例如图1.png
*/
ipcMain.on('save-as', function (event, url, fileName) {
let win = BrowserWindow.getFocusedWindow();
//通过扩展名识别文件类型
let filters = [{ name: '全部文件', extensions: ['*'] }]
let ext = path.extname(fileName) //获取扩展名
if (ext && ext !== '.') {
const name = ext.slice(1, ext.length)
if (name) {
filters.unshift({
name: '',
extensions: [name]
})
}
}
let filePath = null //用户选择存放文件的路径
//1- 弹出另存为弹框,用于获取保存路径
dialog
.showSaveDialog(win, {
title: '另存为',
filters,
defaultPath: fileName
})
.then((result) => {
//点击保存后开始下载
filePath = result.filePath
if (filePath) {
win.webContents.downloadURL(url) // 触发will-download事件
}
})
.catch(() => {
console.log('另存为--catch')
})
//2- 准备下载的时候触发
win.webContents.session.once('will-download', (event, item, webContents) => {
if (!filePath) return
//设置下载项的保存文件路径
item.setSavePath(filePath)
}) })
}) })

View File

@ -8,9 +8,18 @@ export const getSmarttalkPage = (params) => {
params params
}) })
} }
export function deleteSmarttalk(id) { export function deleteSmarttalk(id) {
return request({ return request({
url: '/smarttalk/file/' + id, url: '/smarttalk/file/' + id,
method: 'delete' method: 'delete'
}) })
} }
export const updateSmarttalk = (params) => {
return request({
url: '/smarttalk/file/updateSmarttalk',
method: 'post',
params
})
}

View File

@ -1,5 +1,5 @@
<template> <template>
<el-popover placement="left-end" width="300px" trigger="click"> <el-popover placement="left-end" width="300px" title="文件上传" trigger="click">
<template #default> <template #default>
<el-upload <el-upload
ref="talk_uploader_core" ref="talk_uploader_core"
@ -22,7 +22,7 @@
<div class="talk-uploader-body"> <div class="talk-uploader-body">
<div v-for="(item, index) in uploaderStore.uploadList" :key="index"> <div v-for="(item, index) in uploaderStore.uploadList" :key="index">
<div class="prepare-body-main-item"> <div class="prepare-body-main-item">
<div class="prepare-uploader-progress" :style="{'width': item.percentage+'%'}"></div> <div class="prepare-uploader-progress" :style="{ width: item.percentage + '%' }"></div>
<div class="prepare-body-main-item-icon"> <div class="prepare-body-main-item-icon">
<svg <svg
class="icon" class="icon"
@ -35,11 +35,11 @@
</svg> </svg>
</div> </div>
<div class="prepare-body-main-item-info"> <div class="prepare-body-main-item-info">
<div class="prepare-item-info-title">平面向量基本定理及坐标表示</div> <div class="prepare-item-info-title">{{ item.raw.name }}</div>
<div class="prepare-item-info-message"> <div class="prepare-item-info-message">
<div>1.6MB</div> <div>{{formatFileSize(item.raw.size)}}</div>
&nbsp;&nbsp;|&nbsp;&nbsp; <!-- &nbsp;&nbsp;|&nbsp;&nbsp;-->
<div>古诗词诵读 > 静女</div> <!-- <div>古诗词诵读 > 静女</div>-->
</div> </div>
</div> </div>
<div class="prepare-body-main-item-tool" @click="removeUploadFile(item.uid)"> <div class="prepare-body-main-item-tool" @click="removeUploadFile(item.uid)">
@ -99,6 +99,23 @@ export default {
}, 1000) }, 1000)
}, },
methods: { methods: {
formatFileSize(fileSize) {
if (fileSize < 1024) {
return fileSize + 'B'
} else if (fileSize < 1024 * 1024) {
let temp = fileSize / 1024
temp = temp.toFixed(2)
return temp + 'KB'
} else if (fileSize < 1024 * 1024 * 1024) {
let temp = fileSize / (1024 * 1024)
temp = temp.toFixed(2)
return temp + 'MB'
} else {
let temp = fileSize / (1024 * 1024 * 1024)
temp = temp.toFixed(2)
return temp + 'GB'
}
},
onSuccess(res, file, files) { onSuccess(res, file, files) {
this.removeUploadFile(file.uid) this.removeUploadFile(file.uid)
file.callback(res) file.callback(res)
@ -158,6 +175,7 @@ export default {
.talk-uploader-body { .talk-uploader-body {
width: 100%; width: 100%;
height: 300px; height: 300px;
overflow: auto;
} }
.prepare-body-main-item { .prepare-body-main-item {
position: relative; position: relative;

View File

@ -62,24 +62,24 @@
</div> </div>
</div> </div>
<div class="prepare-body-main-item-tool"> <div class="prepare-body-main-item-tool">
<el-popover placement="left-start" popper-class="prepare-popper" trigger="click"> <el-popover placement="left-start" :hide-after="100" :ref="'popover_'+index" popper-class="prepare-popper" trigger="click">
<template #default> <template #default>
<div style="width: 100%;"> <div style="width: 100%;">
<div class="item-popover"> <div class="item-popover">
<div class="item-popover-item"> <div class="item-popover-item">
<el-button text> <el-button text @click="editTalk(item,index)">
<i class="iconfont icon-bianji"></i> <i class="iconfont icon-bianji"></i>
<span>编辑</span> <span>重命名</span>
</el-button> </el-button>
</div> </div>
<div class="item-popover-item"> <div class="item-popover-item">
<el-button text @click="deleteSmarttalk(item.id)"> <el-button text @click="deleteTalk(item);closePopver(index)">
<i class="iconfont icon-shanchu"></i> <i class="iconfont icon-shanchu"></i>
<span>删除</span> <span>删除</span>
</el-button> </el-button>
</div> </div>
<div class="item-popover-item"> <div class="item-popover-item">
<el-button text> <el-button text @click="downloadFile(item)">
<i class="iconfont icon-xiazai"></i> <i class="iconfont icon-xiazai"></i>
<span>下载</span> <span>下载</span>
</el-button> </el-button>
@ -111,8 +111,9 @@ import ResoureSearch from '@/views/resource/container/resoure-search.vue'
import uploadDialog from '@/components/upload-dialog/index.vue' import uploadDialog from '@/components/upload-dialog/index.vue'
import { Refresh } from '@element-plus/icons-vue' import { Refresh } from '@element-plus/icons-vue'
import uploaderState from '@/store/modules/uploader' import uploaderState from '@/store/modules/uploader'
import { getSmarttalkPage } from '@/api/file' import { deleteSmarttalk, getSmarttalkPage, updateSmarttalk } from '@/api/file'
import { toTimeText } from '@/utils/date' import { toTimeText } from '@/utils/date'
import { ElMessage, ElMessageBox } from 'element-plus'
// import { getSmarttalkPage } from '@/api/file' // import { getSmarttalkPage } from '@/api/file'
const { ipcRenderer } = window.electron || {} const { ipcRenderer } = window.electron || {}
export default { export default {
@ -122,6 +123,7 @@ export default {
return { return {
isLoading: false, isLoading: false,
isDialogOpen: false, isDialogOpen: false,
showToolVisible: false,
fileList: [], fileList: [],
currentNode: {}, currentNode: {},
currentFileList: [], currentFileList: [],
@ -173,6 +175,38 @@ export default {
return temp + 'GB' return temp + 'GB'
} }
}, },
editTalk(item) {
ElMessageBox.prompt('请输入新的名称', '重命名', {
confirmButtonText: '确认',
cancelButtonText: '取消',
inputValue: item.fileShowName.substring(0,item.fileShowName.lastIndexOf('.')),
})
.then(({ value }) => {
item.fileShowName = value;
updateSmarttalk({id:item.id,fileShowName:item.fileShowName}).then(res=>{
if (res.data===true) {
ElMessage({
type: 'success',
message: `修改成功!`,
})
}
})
})
.catch(() => {
})
},
downloadFile(item) {
ipcRenderer.send('save-as',item.fileFullPath,item.fileShowName)
},
deleteTalk(item) {
deleteSmarttalk(item.id).then(res=>{
let index = this.currentFileList.indexOf(item);
this.currentFileList.splice(index,1)
})
},
closePopver(index){
this.$refs['popover_'+index][0].hide();
},
submitFile(files) { submitFile(files) {
let _this = this; let _this = this;
files.filter(file => { files.filter(file => {