oss储存文件

This commit is contained in:
lSun 2025-05-22 14:33:20 +08:00
parent 93bbe815fb
commit 0f766870cc
1 changed files with 66 additions and 0 deletions

View File

@ -1,3 +1,4 @@
const token = localStorage.getItem("token");
layui.config({
base: '../../js/layuiModules/', // 第三方模块所在目录
version: 'v1.6.4' // 插件版本号
@ -414,6 +415,26 @@ function getFileList() {
},
toolbar: "#toolbar"
});
//监听工具条
table.on('tool(fileDemo)', function (obj) {
var data = obj.data; //当前行数据
var fileName = data.fileName;
var filePath = data.filePath;
var rowIndex = obj.index;
var layEvent = obj.event; //当前点击的事件名
if (layEvent === 'download') {
downloads(fileName,filePath)
}
if (layEvent === 'del') {
var index = layer.confirm('确定要删除吗?', {
btn : [ '确定', '取消' ]
}, function() {
layer.close(index);
ajaxCommonMethod('/proProgress/delById',{'id': data.id},"删除成功","删除失败");
});
}
});
}
function layerOpenFormForSecond(title, contentUrl) {
@ -521,3 +542,48 @@ function reloadTip(tip,message,type){
message: message
});
}
function downloads(fileName,filePath){
const url = ctxPath + `/proProgress/download?filePath=${encodeURIComponent(filePath)}` +`&token=`+token;
fetch(url)
.then(response => {
if (!response.ok) throw new Error('下载失败');
return response.blob();
})
.then(blob => {
const downloadUrl = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = downloadUrl;
link.download = fileName; // 设置下载时显示的文件名
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(downloadUrl); // 释放内存
})
.catch(error => {
console.error('文件下载出错:', error);
alert('下载失败,请重试');
});
}
function ajaxCommonMethod(url,data,success,error){
$.ajax({
type: 'POST',
async: false, // 默认异步true,false表示同步
url: ctxPath + url,// 请求地址
contentType: "application/x-www-form-urlencoded",
dataType: 'json', // 服务器返回数据类型
data: data, //获取提交的表单字段
success: function (data) {
var resMsg = data.resMsg;
if ("数据获取成功" == resMsg) {
reloadTip("删除",success,'success');
setTimeout(function(){
getFileList();
}, 2000);
}else {
reloadTip("删除",error,"error");
}
}
});
}