<template>
<div>
<el-upload
v-loading="loading"
class="upload-demo"
action="*"
:on-remove="fileRemove"
:on-change="fileChang"
:http-request="uploadFile"
accept=""
:before-upload="changebefore"
:auto-upload="true"
:multiple="true"
:file-list="form.instFilePics"
ref="uploadFile">
<el-button style="width: 200px" size="mini" type="primary">
<i class="el-icon-folder-opened"></i>上传
<!-- 文件夹上传-->
</el-button>
</el-upload>
</div>
</template>
<script>
import SimpleTime from "../SimpleTime";
import {batchUpload} from '@/utils/api.js'
export default {
name: "index.vue",
data() {
return {
loading:false,
form: {
instFilePics: [],
},
'
}
},
components: {SimpleTime},
mounted() {
//以下代码,有时候可能写法不同,可在控制台打印一层一层的找input,再给加webkitdirectory 属性
this.$refs.uploadFile.$children[0].$refs.input.webkitdirectory = true;
},
methods: {
changebefore(){
if (this.time.year=="" || this.frequency =='') {
this.$message({
type: 'info',
message: '请选择时间'
});
return false;
}
},
fileChang(file, fileList) {
this.form.instFilePics = fileList;
},
fileRemove(file, fileList) {
this.form.instFilePics = fileList
},
uploadFile() {
let formData = new FormData()
this.form.instFilePics.forEach(item => {
formData.append('files', item.raw)
})
formData.append('time', JSON.stringify(this.time))
formData.append('frequency', this.frequency)
this.loading=true
batchUpload(formData).then(res => {
if (res && res.code === 200) {
this.$message.success(res.message)
this.loading=false
} else if(res && res.code ==-2) {
this.$message.warning(res.message)
this.loading=false
}else{
this.$message.error(res.message)
this.loading=false
}
})
},
}
}
</script>
<style scoped>
.upload-demo {
margin-top: 20px;
}
</style>
多文件上传,文件夹上传
最新推荐文章于 2024-04-26 16:47:03 发布