先在请求头中设置上传表单数据
//在请求头中设置
if(config.url == "/img_upload" || config.url == "/img_uploads" || config.url == "/batch_txt"){
config.headers = { "Content-Type": "multipart/form-data"}
return config
}
请求中定义formData数据
//e就是上传的二进制数据
const froms = new FormData()
froms.append('txt',e)
froms.append('token',this.$store.state.token)
this.$http.post('/request',froms).then((res) => {
})
上传图片时图片显示,里面new FileReader 就是读取文件,file就是组件上传返回的file数据
beforeUpdate(file){
const reader = new FileReader()
reader.readAsDataURL(file)
//uploadImg 就是上传的图片
reader.onload=res=>{
this.uploadImg = res.target.result
}
fd.append('img', file)
},