<el-button type="primary" v-permission="'Compilation_import'" @click="importClick"> <input ref="fileInputRef" type="file" accept=".xlsx, .xls" style="display: none;" @change="handleChange"> 导入 </el-button>
nst fileInputRef = ref(null);
const importClick = ()=>{ fileInputRef.value.value = ''; fileInputRef.value.click() };
const handleChange = async (event) => { const formData = new FormData(); formData.append('file', fileInputRef.value.files[0]); try { ElMessageBox.confirm( '确定默认参数配置,确定导入该批次<+'+addformData3.value.batch+'>图书', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning', } ).then( async () => { const res = await axios.post('/compilation/importBookList', formData, { headers: { 'Content-Type': 'multipart/form-data' } }); if (res.status === 200) { ElMessage.success('文件上传成功:'); }else { ElMessage.success('文件上传失败:'); } } ) } catch (error) { console.error('文件上传失败:', error); } fileInputRef.value.reset(); }
后端接收
public ApiResult import(@RequestParam("file") MultipartFile file){}