当前位置: 代码迷 >> 综合 >> vant vue单张图片上传
  详细解决方案

vant vue单张图片上传

热度:33   发布时间:2023-11-07 01:54:59.0

上传组件

 <template #input><van-uploader accept="file" :max-count="1" v-model="photo" :after-read="afterPhoto" /></template>

data

data() {
    return {
    photo: [],}
},

事件绑定,组装成file文件

 afterPhoto(file) {
    const tempFile = file.fileconst type = tempFile.typeconst size = tempFile.sizeconst newName = new Date().getTime() + tempFile.namelet uploadFile = new File([tempFile], newName, {
    type,size})let formData = new FormData()formData.append('image', uploadFile)this.$axios.post(this.apiUrl + '/php/upload', formData).then((res) => {
    this.photo.url = this.apiUrl + res.data.data.image}).catch((err) => {
    console.log(err);});},
  相关解决方案