1.axios封装
http.postFormData = function(vm, url, params) {
return new Promise((resolve) => {
axios.post(url, params, {
headers: {
'Content-Type': 'multipart/form-data',
'Authorization': store.state.token
},
timeout: config.httpTimeOut
})
.then(res => {
custResponseFun(vm, res, resolve);
})
.catch(err => {
custErrFun(vm, err);
});
});
};
2.template模板
form style="display: none">
<input type="file" name="fileInput" id="fileInput" ref="fileRef" @change="getAddFile"
accept=".csv, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
</form>
3.JS代码
const { proxy } = getCurrentInstance();
const fileRef = ref(null);
/*上传文档*/
const importData = () => {
fileRef.value.click();
}
/**上传文件 */
const getAddFile = (e) => {
let file = fileRef.value.files[0];
console.log(file);
let fm= new FormData();
fm.append("file", file);
proxy.$H
.postFormData(proxy, proxy.$A.prediction.importTemplate, fm)
.then(res => {
console.log(res);
})
}
4.效果