Said that under the idea, ant own a-upload component sets a bad drag, because we need to drag that fileList, so I put the component's own fileList into an empty, wrote a 'fileList', so we filelist cycle out many operations can be carried out with the data itself.
1. I am the a-upload wrote a component
used in the assembly in vuedraggable drag and drop
install npm install vuedraggable
new component files, paths: src / components / UploadImage / UploadImage.vue
components see the code
<template>
<div class="myFileList">
<a-spin :spinning="confirmLoading">
<a-upload
listType="picture"
class="upload-list-inline"
:fileList="[]"
:customRequest="uploadImage"
:beforeUpload="beforeUpload"
:multiple="true"
>
<a-button>
<a-icon type="upload" /> 上传附件
</a-button>
</a-upload>
<div class="ant-upload-list ant-upload-list-picture">
<draggable
class="syllable_ul"
element="ul"
:list="fileList"
:options="{group:'title', animation:150}"
:no-transition-on-drag="true"
@change="change"
>
<transition-group type="transition" :name="!drag? 'syll_li' : null" :css="true">
<div class="ant-upload-list-item ant-upload-list-item-done" v-for="(item, index) in fileList" :key="index">
<div class="ant-upload-list-item-info">
<span><a :href="item.url" target="_blank" rel="noopener noreferrer" class="ant-upload-list-item-thumbnail"><img :src="item.url" alt="4.jpg"></a><a target="_blank" rel="noopener noreferrer" title="4.jpg" :href="item.url" class="ant-upload-list-item-name">{{item.name}}</a></span>
</div>
<i aria-label="图标: close" tabindex="-1" class="anticon anticon-close" @click="removeCoupon(item.uid)" ><svg viewBox="64 64 896 896" data-icon="close" width="1em" height="1em" fill="currentColor"><path d="M563.8 512l262.5-312.9c4.4-5.2.7-13.1-6.1-13.1h-79.8c-4.7 0-9.2 2.1-12.3 5.7L511.6 449.8 295.1 191.7c-3-3.6-7.5-5.7-12.3-5.7H203c-6.8 0-10.5 7.9-6.1 13.1L459.4 512 196.9 824.9A7.95 7.95 0 0 0 203 838h79.8c4.7 0 9.2-2.1 12.3-5.7l216.5-258.1 216.5 258.1c3 3.6 7.5 5.7 12.3 5.7h79.8c6.8 0 10.5-7.9 6.1-13.1L563.8 512z"></path></svg></i>
</div>
</transition-group>
</draggable>
</div>
</a-spin>
</div>
</template>
<script>
import { axios } from '@/utils/request'
import draggable from 'vuedraggable'
export default {
name: 'UploadImage',
props: {
multiple: {
type: Number,
default: 1
},
img: {
type: Array,
default: function () {
return []
}
}
},
data () {
return {
fileList: [],
imageId: [],
files: [],
a: [],
confirmLoading: false,
drag: false
}
},
components: {
draggable
},
methods: {
change (evt) {
},
beforeUpload (file) {
const isJPG = file.type === 'image/jpeg' || file.type === 'image/jpg' || file.type === 'image/png' || file.type === 'image/bmp'
if (!isJPG) {
this.$notification['error']({
message: '请上传图片文件'
})
}
// const isLt2M = file.size / 1024 < 200 && file.size / 1024 > 10;
// if (!isLt2M) {
// this.$message.error('文件大小应在10KB~20KB之间');
// }
// return isJPG && isLt2M
return isJPG
},
removeCoupon: function (file) {
this.fileList.forEach((val, key) => {
if (file === val.uid) {
this.fileList.splice(key, 1)
this.$emit('delimageId', key)
}
})
},
// 图片预览
uploadImg: function (e) {
let _this = this
if (!e || !window.FileReader) return
let reader = new FileReader()
reader.readAsDataURL(e.file)
reader.onloadend = function () {
_this.uploadImage()
}
},
uploadImage: function (e) {
this.confirmLoading = true
let formdata = new FormData()
formdata.append('file', e.file)
axios({
url: '/api/file/upload',
method: 'post',
data: formdata
}).then((res) => {
this.confirmLoading = false
if (this.multiple === 1) {
this.fileList = [{
uid: '-1', // 文件唯一标识,建议设置为负数,防止和内部产生的 id 冲突
name: e.file.name, // 文件名
status: 'done', // 状态有:uploading done error removed
url: this.GLOBAL.imgUrl + res.result.fileId
}]
} else {
if (this.fileList.length < this.multiple) {
this.fileList.push({
uid: '-' + (this.fileList.length + 1), // 文件唯一标识,建议设置为负数,防止和内部产生的 id 冲突
name: e.file.name, // 文件名
status: 'done', // 状态有:uploading done error removed
url: this.GLOBAL.imgUrl + res.result.fileId
})
}
}
this.$emit('getImageId', res.result.fileId)
})
}
},
mounted () {
if (this.img.length > 0) {
this.img.forEach((val, key) => {
this.fileList.push({
uid: '-' + key, // 文件唯一标识,建议设置为负数,防止和内部产生的 id 冲突
name: '' + val.name, // 文件名
status: 'done', // 状态有:uploading done error removed
url: '' + val.url
})
})
}
}
}
</script>
<style>
ul{
padding: 0;
}
</style>
2.
Import UploadImage from '... / ... / ... / Components / UploadImage / UploadImage'
Components: {
UploadImage
},
(. 1) add a page, does not need echo image
<upload-image @getImageId="getImageId" @delimageId="delimageId" :multiple="1"></upload-image>
// 看到multiple了吗,后面的数字为改组件允许最多传多少张,如果不限制图片张数的话,写999就可以了
// 产品封面图片预览
getImageId: function (val) {
this.imageId.push(val)
},
// 删除产品封面图片
delimageId: function (index) {
this.imageId.forEach((val, key) => {
if (index === key) {
this.imageId.splice(key, 1)
}
})
},
// 这里两个方法获取的imageId,是我晚点要传给后台的数据
(2) edit page, you need to echo picture
single echo:
<upload-image @getImageId="getImageId" @delimageId="delimageId" :multiple="1" :img="[{name: proDel.title, url: imgUrl}]" v-if="lock"></upload-image>
// 图片预览
getImageId: function (val) {
this.imageId = []
this.imageId.push(val)
},
// 删除图片
delimageId: function (index) {
this.imageId.forEach((val, key) => {
if (index === key) {
this.imageId.splice(key, 1)
}
})
},
多张回显:
<upload-image @getImageId="getDelImageId" @delimageId="delDelimageId" :multiple="99" :img="showContent" v-if="lock"></upload-image>
if (!res.result.content) {
this.showContent = []
} else {
var ss = res.result.content.split(',')
this.showContent = ss.map(item => ({ name: res.result.title, url: this.GLOBAL.imgUrl + item }))
}
//res.result.content 是我从接口获取的数据,把数据拼接一下,搞成我们需要的格式
// 产品详情图片预览
getDelImageId: function (val) {
this.imageDelId.push(val)
},
// 删除产品详情封面图片
delDelimageId: function (index) {
this.imageDelId.forEach((val, key) => {
if (index === key) {
this.imageDelId.splice(key, 1)
}
})
},
// use the above two, there are components in a v-if = "lock" did not see this lock, initially false, wait until we get all the data from the interface, and then set to true
Finally, I have a question, under the window, this component will occasionally get out of the picture, it is good to refresh, is below this figure, I use mac, I did not come across, so I do not know how to solve this oh