1、在WebUploader的官網去下載文件包,因爲項目基於jquery所以也要下載jquery文件,然後在放在public下新建的webUpload文件夾下。
2、在index.html的body後面引入jquer和插件js
3、vue3.0 在vue中進行封裝
<template>
<div class="upload"></div>
</template>
<script>
export default {
name: 'vue-upload',
props: {
accept: {
type: Object,
default: {},
},
// 上傳地址
url: {
type: String,
default: '',
},
// 上傳文件名
fileName: {
type: String,
default: '',
},
// 上傳最大數量 默認爲10
fileNumLimit: {
type: Number,
default: 10,
},
// 大小限制 默認1M
fileSingleSizeLimit: {
type: Number,
default:1024000,
},
// 上傳時傳給後端的參數,一般爲token,key等
formData: {
type: Object,
default: {}
},
// 生成formData中文件的key,下面只是個例子,具體哪種形式和後端商議
keyGenerator: {
type: Function,
default(file) {
const currentTime = new Date().getTime();
const key = `${currentTime}.${file.name}`;
return key;
},
},
multiple: {
type: Boolean,
default: false,
},
// 上傳按鈕ID
uploadButton: {
type: String,
default: '',
},
},
data() {
return {
uploader: null
};
},
mounted() {
this.initWebUpload();
},
methods: {
initWebUpload() {
this.uploader = WebUploader.create({
auto: true, // 選完文件後,是否自動上傳
swf: '/public/webUploader/Uploader.swf', // swf文件路徑
server: this.url, // 文件接收服務端
fileVal : this.fileName,
pick: {
id: this.uploadButton, // 選擇文件的按鈕
multiple: this.multiple, // 是否多文件上傳 默認false
label: '',
},
accept: this.getAccept(this.accept), // 允許選擇文件格式。
threads: 3,
fileNumLimit: this.fileNumLimit, // 限制上傳個數
fileSingleSizeLimit: this.fileSingleSizeLimit, // 限制單個上傳圖片的大小
formData: this.formData, // 上傳所需參數
chunked: true, //分片上傳
chunkSize: 1024000*this.fileSingleSizeLimit, //分片大小
duplicate: true, // 重複上傳
compress:false,
});
this.uploader.on('beforeFileQueued',(file)=>{
this.$emit('beforeFile', file);
})
// 當有文件被添加進隊列的時候,添加到頁面預覽
this.uploader.on('fileQueued', (file) => {
this.$emit('fileChange', file);
});
this.uploader.on('uploadStart', (file) => {
// 在這裏可以準備好formData的數據
this.uploader.options.formData.dirname = file.source.source.webkitRelativePath;
});
// 文件上傳過程中創建進度條實時顯示。
this.uploader.on('uploadProgress', (file, percentage) => {
this.$emit('progress', file, percentage);
});
this.uploader.on('uploadSuccess', (file, response) => {
this.$emit('success', file, response);
});
this.uploader.on('uploadError', (file, reason) => {
this.$emit('uploadError', file, reason);
});
this.uploader.on('error', (type) => {
let errorMessage = '';
if (type === 'F_EXCEED_SIZE') {
errorMessage = `已阻止大小超過${this.fileSingleSizeLimit / (1024 * 1000)}M的文件`;
} else if (type === 'Q_EXCEED_NUM_LIMIT') {
errorMessage = `文件上傳已達到最大上限數,已阻止超過${this.fileNumLimit}張的文件`;
} else {
errorMessage = `上傳出錯!請檢查後重新上傳!錯誤代碼${type}`;
}
this.$emit('error', errorMessage);
});
this.uploader.on('uploadComplete', (file, response) => {
this.$emit('complete', file, response);
});
},
upload(file) {
this.uploader.upload(file);
},
stop(file) {
this.uploader.stop(file);
},
// 取消並中斷文件上傳
cancelFile(file) {
this.uploader.cancelFile(file);
},
// 在隊列中移除文件
removeFile(file, bool) {
this.uploader.removeFile(file, bool);
},
getAccept(accept) {
switch (accept) {
case 'text':
return {
title: 'Texts',
exteensions: 'doc,docx,xls,xlsx,ppt,pptx,pdf,txt',
mimeTypes: '.doc,docx,.xls,.xlsx,.ppt,.pptx,.pdf,.txt'
};
break;
case 'video':
return {
title: 'Videos',
exteensions: 'mp4',
mimeTypes: '.mp4'
};
break;
case 'image':
return {
title: 'Images',
exteensions: 'gif,jpg,jpeg,bmp,png',
mimeTypes: '.gif,.jpg,.jpeg,.bmp,.png'
};
break;
default: return accept
}
},
},
};
</script>
<style lang="scss">
.webuploader-container {
position: relative;
}
.webuploader-element-invisible {
position: absolute !important;
clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
clip: rect(1px,1px,1px,1px);
}
.webuploader-pick {
position: relative;
display: inline-block;
cursor: pointer;
color: #00faff;
text-align: center;
overflow: hidden;
}
.webuploader-pick-hover {
}
.webuploader-pick-disable {
opacity: 0.6;
pointer-events:none;
}
</style>
4、在父組件中使用
//template
<button id="filePicker" >選擇文件夾</button>
<vue-upload
ref="uploader"
:accept='accept'
:fileName='fileName'
:formData='formData'
:url="uploadUrl"
uploadButton="#filePicker"
multiple
@success='onFileSuccess'
@uploadError='uploadError'
@error="onError"
></vue-upload>
//data
uploadUrl:base.baseURL+'/api/pic/upload',
formData:{},
accept:{ //接收文件類型
title: 'Images',
extensions: 'gif,jpg,jpeg,bmp,png',
mimeTypes: 'image/*'
},