以创建元素的形式 创建的input标签,多次上传后input里面还会保留之前file的数据,应该如何操作删除,感谢各位
uploadFile(el, value, field, indexId, level, type) {
console.log(
el,
value,
field,
indexId,
level,
type,
'uploadfile==========='
)
if (el && el.target && el.target.files && el.target.files.length > 0) {
// this.files = el.target.files[0]
this.files.push(el.target.files[0])
console.log(this.files, 'el.target.files')
const files = el.target.files[0]
const isLt2M = files.size / 1024 / 1024 < 2
const size = files.size / 1024 / 1024
console.log(size)
// 判断上传文件的大小
if (!isLt2M) {
this.$message.error('上传头像图片大小不能超过 2MB!')
} else if (files.type.indexOf('image') === -1) {
//如果不是图片格式
// this.$dialog.toast({ mes: '请选择图片文件' });
this.$message.error('请选择图片文件')
} else {
let that = this
const reader = new FileReader() // 创建读取文件对象
reader.readAsDataURL(el.target.files[0]) // 发起异步请求,读取文件
// console.log(window.URL.createObjectURL(files),'files1111111111111111111111')
// this.previewImage = window.URL.createObjectURL(files);
reader.onload = function() {
// 文件读取完成后
// 读取完成后,将结果赋值给img的src
that.apiImgLoopList.image = URL.createObjectURL(files)
that.uploadType = '图片上传完成'
console.log(document.getElementById('imgs').value, 'imgssssss')
document.getElementById('imgs').value = ''
if (that.files.length > 0) {
if (that.typeStatus == '编辑') {
let formData2 = new FormData()
console.log(that.files,'that.filesthat.filesthat.filesthat.files')
that.files.forEach((item) => {
formData2.append('url', item)
})
uploadImg(formData2, 'product').then((response) => {
if (response.status == 200) {
// this.$message.success('上传成功')
response.data.forEach((item, index) => {
that.uploadSpecArr[index].image = item.url
})
} else {
that.$message.error(response.message)
}
that.clickTag(
that.clickTagValue,
that.clickTagField,
that.clickTagIndexId,
that.clickTagLevel,
that.clickTagType,
that.uploadType
)
})
} else {
// 数据传到后台
let formData2 = new FormData()
console.log(that.files,'只有一张的that.files')
that.files.forEach((item) => {
formData2.append('url', item)
})
// formData2.append('image', this.files) // 可以传到后台的数据
console.log(formData2, '2222222222222222222222222')
uploadImg(formData2, 'product').then((response) => {
if (response.status == 200) {
// this.$message.success('上传成功')
// that.uploadSpecArr = response.data;
response.data.forEach((item, index) => {
that.uploadSpecArr[index].image = item.url
})
} else {
that.$message.error(response.message)
}
that.clickTag(
that.clickTagValue,
that.clickTagField,
that.clickTagIndexId,
that.clickTagLevel,
that.clickTagType,
that.uploadType
)
})
}
// 数据传到后台
}
// document.getElementById('uploadImgs').value=''
// that.clickTag(
// that.clickTagValue,
// that.clickTagField,
// that.clickTagIndexId,
// that.clickTagLevel,
// that.clickTagType,
// that.uploadType
// )
}
}
}
},
el.value = ''