官网给的例子action
都是绝对地址,我现在需要上传到自己后台的地址,只有一个路由地址/task/upload
根据 config/index.js
配置,那么action
要写成/api/task/upload
,另外也可以传入函数来返回地址:action="uploadUrl()"
。
另外一个问题就是此插件不会默认带上token,需要手动传递。
<el-upload class="upload-demo"
action="/api/task/upload"
:multiple="false"
name="jarfile"
:file-list="fileList"
accept="application/java-archive"
:on-success="onUploadSuccess"
:headers="{'Auth-Token': tokenValue}">
<el-button size="small" type="primary">点击上传</el-button>
<span slot="tip" class="el-upload__tip" style="margin-left: 10px;">不要重复上传文件,按需上传</span>
</el-upload>
<script>
import store from '@/store'
export default {
data() {
return {
tokenValue: store.getters.user.token,
fileList: []
}
},
computed: {},
components: {},
created() {},
methods: {
onUploadSuccess(response, file, fileList) {
this.fileList = []
if (response.code === 0) {
this.form.command = response.data
// this.fileList = [{ name: response.data, url: response.data, status: 'success' }]
} else {
this.$message.error(response.message)
}
}
}
}
</script>