Commit 4178a60e authored by sxl's avatar sxl 💬

fix:解决upload上传图片请求404

parent d0ac0705
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
<!-- 上传按钮 --> <!-- 上传按钮 -->
<el-button size="mini" type="primary">选取文件</el-button> <el-button size="mini" type="primary">选取文件</el-button>
<div slot="file" slot-scope="{ file }"> <div slot="file" slot-scope="{ file }">
<img class="el-upload-list__item-thumbnail" :src="baseUrl + file.url" alt /> <img class="el-upload-list__item-thumbnail" :src="file.url" alt />
<span class="el-upload-list__item-actions"> <span class="el-upload-list__item-actions">
<span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)"> <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
<i class="el-icon-zoom-in"></i> <i class="el-icon-zoom-in"></i>
...@@ -113,8 +113,29 @@ export default { ...@@ -113,8 +113,29 @@ export default {
}, },
dialogImageUrl: '', dialogImageUrl: '',
fileList: [], fileList: [],
// 添加标志位
isInternalChange: false,
} }
}, },
watch: {
// 回显
value: {
handler(newVal, oldVal) {
// 只有当变化不是由组件内部引起时,才处理
if (!this.isInternalChange) {
if (newVal) {
this.fileList = newVal.split(',').map((url) => ({ url: this.baseUrl + url }))
} else {
this.fileList = []
}
}
// 重置标志位
this.isInternalChange = false
},
deep: true,
immediate: true,
},
},
mounted() { mounted() {
if (this.drag && !this.disabled) { if (this.drag && !this.disabled) {
this.$nextTick(() => { this.$nextTick(() => {
...@@ -131,22 +152,6 @@ export default { ...@@ -131,22 +152,6 @@ export default {
}) })
} }
}, },
watch: {
value: {
handler(val) {
this.fileList = []
if (val) {
this.$nextTick(() => {
val.split(',').forEach((url) => {
this.fileList.push({ url })
})
})
}
},
deep: true,
immediate: true,
},
},
computed: { computed: {
// 是否显示提示 // 是否显示提示
showTip() { showTip() {
...@@ -196,12 +201,6 @@ export default { ...@@ -196,12 +201,6 @@ export default {
handleUploadSuccess(res, file) { handleUploadSuccess(res, file) {
if (res.code === 200) { if (res.code === 200) {
this.uploadList.push({ name: res.fileName, url: res.fileName }) this.uploadList.push({ name: res.fileName, url: res.fileName })
const updatedFile = this.$refs.fileUpload.uploadFiles.find((f) => f.uid === file.uid)
if (updatedFile) {
updatedFile.url = res.fileName
updatedFile.name = res.fileName
}
this.uploadedSuccessfully() this.uploadedSuccessfully()
} else { } else {
this.number-- this.number--
...@@ -212,10 +211,15 @@ export default { ...@@ -212,10 +211,15 @@ export default {
} }
}, },
handleRemove(file) { handleRemove(file) {
this.uploadList.forEach((item, index) => {
if (item.url === file.url) {
const updatedFile = this.$refs.fileUpload.uploadFiles const updatedFile = this.$refs.fileUpload.uploadFiles
updatedFile.forEach((item, index) => {
if (item.url === file.url) {
// 从组件上传列表中移除文件
updatedFile.splice(index, 1) updatedFile.splice(index, 1)
// 从自己的上传列表中移除文件
this.uploadList.splice(index, 1)
// 在触发 input 事件前设置标志位
this.isInternalChange = true
this.$emit('handleRemove', this.listToString(this.uploadList)) this.$emit('handleRemove', this.listToString(this.uploadList))
} }
}) })
...@@ -227,26 +231,13 @@ export default { ...@@ -227,26 +231,13 @@ export default {
handleDownload(file) { handleDownload(file) {
console.log(file) console.log(file)
}, },
// 删除文件
handleDelete(index) {
this.fileList.splice(index, 1)
this.$emit('input', this.listToString(this.fileList))
},
// 上传结束处理 // 上传结束处理
uploadedSuccessfully() { uploadedSuccessfully() {
this.number = 0 // 在触发 input 事件前设置标志位
this.$emit('input', this.listToString(this.fileList)) this.isInternalChange = true
this.$emit('input', this.listToString(this.uploadList))
this.$modal.closeLoading() this.$modal.closeLoading()
}, },
// 获取文件名称
getFileName(name) {
// 如果是url那么取最后的名字 如果不是直接返回
if (name.lastIndexOf('/') > -1) {
return name.slice(name.lastIndexOf('/') + 1)
} else {
return name
}
},
// 对象转成指定字符串分隔 // 对象转成指定字符串分隔
listToString(list, separator) { listToString(list, separator) {
let strs = '' let strs = ''
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment