Vue使用el-upload上传JSON,TXT文件解析后进行传递
<template><div><el-uploadaction:on-change="updateReason":auto-upload="false"accept=".json"list-type="multipart/form-data"><el-button size="small" type="primary">阶段原因上传</el-b
·
<template>
<div>
<el-upload
action
:on-change="updateReason"
:auto-upload="false"
accept=".json"
list-type="multipart/form-data"
>
<el-button size="small" type="primary">阶段原因上传</el-button>
<div slot="tip" class="el-upload__tip">只能上传JSON文件</div>
</el-upload>
</div>
</template>
<script>
import http from '../../lib/http' //(封装的axios)
export default {
data() {
return {
fileList: [],
}
},
methods: {
updateReason(file, fileList) {
this.fileTransmit(file, fileList)
},
fileTransmit(file, fileList, isShow) {
this.fileList = fileList
var reader = new FileReader() //新建一个FileReader
reader.readAsText(file.raw, 'UTF-8') //读取文件
reader.onload = async function(evt) { //读取文件完毕执行此函数
let dataJson = JSON.parse(evt.target.result)
let apiUrl = 'http://11.2.180.225:3003/searchplat/sample/user/updateReason'
let { data } = await http.post(apiUrl, { json: dataJson })
console.log(data)
}
},
},
}
</script>
更多推荐
已为社区贡献5条内容
所有评论(0)