使用el-upload组件上传,上传是可以了,但是如何读取文件到列表
比如 我有一个主表有着附件,新增时候可以上传了 但是编辑时候如何读取数据到el-upload的上传列表中?
代码如下
'<el-upload
ref="upload"
class="avatar-uploader"
action="#"
:show-file-list="true"
:before-upload="beforeAvatarUpload"
:on-remove="handleRemove"
:http-request="upload"
<el-button slot="trigger" size="small" type="primary">选取文件</el-button>
<el-button size="small" type="primary" @click="test()">测试</el-button>
</el-upload>'
`export default {
model: {
prop: 'fileList',
event: 'change',
},
props: {
fileList: Array,
size: {
type: Number,
default: 1,
},
},
data () {
return {
uploadUrl: 'major/upload/upload', // 图片上传路径 例: '/ueditor/dynamic/imgUpload;
}
},
methods: {
init () {
},
upload (f) {
const file = f.file
const form = new FormData()
form.append('file', file)
this.$http({
url: this.$http.adornUrl(this.uploadUrl),
method: 'post',
data: form,
processData: false,
contentType: false,
dataType: 'json',
async: false,
})
.then(({ data }) => {
if (data && data.code === 200) {
file.dataBaseUrl = data.data
this.fileList.push(file)
} else {
this.$message({
message: '上传文件失败',
type: 'warning',
duration: 1500,
})
this.$refs.upload.uploadFiles.pop()
}
this.loading = false
})
.catch(error => {
console.log(error)
this.loading = false
this.$refs.upload.uploadFiles.pop()
this.$message({
message: '上传文件失败',
type: 'warning',
duration: 1500,
})
})
},
// 上传图片前的过滤
beforeAvatarUpload (file) {
return true
},
test () {
console.log(this.fileList)
},
handleRemove (file) {
this.fileList.splice(this.fileList.findIndex(item => item.uid === file.uid), 1)
},
},
}`
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…