图片上传组件修改

This commit is contained in:
qinjie 2021-11-22 15:53:40 +08:00
parent c55c81c701
commit 8458c5d9e8
1 changed files with 36 additions and 31 deletions

View File

@ -1,14 +1,7 @@
<template>
<div class="clearfix">
<a-upload
list-type="picture-card"
:multiple="false"
:file-list="fileList"
:before-upload="beforeUpload"
:preview-file="previewFile"
:remove="handleRemove"
@preview="handlePreview">
<div v-if="fileList.length < 1">
<a-upload list-type="picture-card" :multiple="false" :file-list="fileList" :accept="accept" :before-upload="beforeUpload" :preview-file="previewFile" :remove="handleRemove" @preview="handlePreview">
<div v-if="fileList.length < maxListSize">
<a-icon type="plus" />
<div class="ant-upload-text">上传</div>
</div>
@ -25,14 +18,14 @@ import { ossUpload } from '@/api/sys/oss'
import axios from 'axios'
import storage from 'store'
import { ACCESS_TOKEN } from '@/store/mutation-types'
// function getBase64(file) {
// return new Promise((resolve, reject) => {
// const reader = new FileReader()
// reader.readAsDataURL(file)
// reader.onload = () => resolve(reader.result)
// reader.onerror = error => reject(error)
// })
// }
function getBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = () => resolve(reader.result)
reader.onerror = error => reject(error)
})
}
export default {
components: {
[Upload.name]: Upload
@ -44,12 +37,20 @@ export default {
},
defaultList: {
type: Array,
default () {
default() {
return []
}
},
maxListSize: {
type: Number,
default: 1
},
accept: {
type: String,
default: null
}
},
data () {
data() {
return {
uploading: false,
fileList: [],
@ -58,13 +59,13 @@ export default {
}
},
methods: {
previewFile (file) {
previewFile(file) {
console.log(file)
},
clear () {
clear() {
this.fileList = []
},
flush () {
flush() {
this.clear()
this.defaultList.forEach((file, index) => {
this.fileList.push({
@ -75,25 +76,29 @@ export default {
})
})
},
handleRemove (file) {
handleRemove(file) {
const index = this.fileList.indexOf(file)
const newFileList = this.fileList.slice()
newFileList.splice(index, 1)
this.fileList = newFileList
},
handleCancel () {
handleCancel() {
this.previewVisible = false
},
beforeUpload (file) {
beforeUpload(file) {
this.fileList = [...this.fileList, file]
return false
},
async handlePreview (file) {
async handlePreview(file) {
console.log(file)
if (!file.url && !file.preview)
file.preview = await getBase64(file.originFileObj);
this.previewImage = file.url
this.previewVisible = true
},
submitUpload () {
submitUpload() {
return new Promise((resolve, reject) => {
const { fileList } = this
if (fileList.length === 0) {