同时支持OSS和MinIO文件上传

This commit is contained in:
macro 2020-01-01 15:30:58 +08:00
parent 97567117aa
commit 6bc6006404
2 changed files with 32 additions and 8 deletions

View File

@ -1,8 +1,8 @@
<template>  <template> 
<div> <div>
<el-upload <el-upload
action="http://macro-oss.oss-cn-shenzhen.aliyuncs.com" :action="useOss?ossUploadUrl:minioUploadUrl"
:data="dataObj" :data="useOss?dataObj:null"
list-type="picture-card" list-type="picture-card"
:file-list="fileList" :file-list="fileList"
:before-upload="beforeUpload" :before-upload="beforeUpload"
@ -44,7 +44,10 @@
host: '' host: ''
}, },
dialogVisible: false, dialogVisible: false,
dialogImageUrl:null dialogImageUrl:null,
useOss:true, //使oss->true;使MinIO->false
ossUploadUrl:'http://macro-oss.oss-cn-shenzhen.aliyuncs.com',
minioUploadUrl:'http://localhost:8080/minio/upload',
}; };
}, },
computed: { computed: {
@ -73,6 +76,10 @@
}, },
beforeUpload(file) { beforeUpload(file) {
let _self = this; let _self = this;
if(!this.useOss){
//使oss
return true;
}
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
policy().then(response => { policy().then(response => {
_self.dataObj.policy = response.data.policy; _self.dataObj.policy = response.data.policy;
@ -89,7 +96,12 @@
}) })
}, },
handleUploadSuccess(res, file) { handleUploadSuccess(res, file) {
this.fileList.push({url: file.name,url:this.dataObj.host + '/' + this.dataObj.dir + '/' + file.name}); let url = this.dataObj.host + '/' + this.dataObj.dir + '/' + file.name;
if(!this.useOss){
//使oss
url = res.data.url;
}
this.fileList.push({name: file.name,url:url});
this.emitInput(this.fileList); this.emitInput(this.fileList);
}, },
handleExceed(files, fileList) { handleExceed(files, fileList) {

View File

@ -1,8 +1,8 @@
<template>  <template> 
<div> <div>
<el-upload <el-upload
action="http://macro-oss.oss-cn-shenzhen.aliyuncs.com" :action="useOss?ossUploadUrl:minioUploadUrl"
:data="dataObj" :data="useOss?dataObj:null"
list-type="picture" list-type="picture"
:multiple="false" :show-file-list="showFileList" :multiple="false" :show-file-list="showFileList"
:file-list="fileList" :file-list="fileList"
@ -62,7 +62,10 @@
host: '', host: '',
// callback:'', // callback:'',
}, },
dialogVisible: false dialogVisible: false,
useOss:true, //使oss->true;使MinIO->false
ossUploadUrl:'http://macro-oss.oss-cn-shenzhen.aliyuncs.com',
minioUploadUrl:'http://localhost:8080/minio/upload',
}; };
}, },
methods: { methods: {
@ -77,6 +80,10 @@
}, },
beforeUpload(file) { beforeUpload(file) {
let _self = this; let _self = this;
if(!this.useOss){
//使oss
return true;
}
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
policy().then(response => { policy().then(response => {
_self.dataObj.policy = response.data.policy; _self.dataObj.policy = response.data.policy;
@ -96,7 +103,12 @@
handleUploadSuccess(res, file) { handleUploadSuccess(res, file) {
this.showFileList = true; this.showFileList = true;
this.fileList.pop(); this.fileList.pop();
this.fileList.push({name: file.name, url: this.dataObj.host + '/' + this.dataObj.dir + '/' + file.name}); let url = this.dataObj.host + '/' + this.dataObj.dir + '/' + file.name;
if(!this.useOss){
//使oss
url = res.data.url;
}
this.fileList.push({name: file.name, url: url});
this.emitInput(this.fileList[0].url); this.emitInput(this.fileList[0].url);
} }
} }