010-58813333 可信网站,网约车app开发,华为手机官方网站登录,西宁seo网站建设在 Vue 3 中#xff0c;如果你想将文件#xff08;例如上传的 Excel 文件#xff09;以 FormData 格式发送到后端#xff0c;可以通过以下步骤实现。这种方式通常用于处理文件上传#xff0c;因为它可以将文件和其他数据一起发送到服务器。
首先#xff0c;创建一个 Vue…在 Vue 3 中如果你想将文件例如上传的 Excel 文件以 FormData 格式发送到后端可以通过以下步骤实现。这种方式通常用于处理文件上传因为它可以将文件和其他数据一起发送到服务器。
首先创建一个 Vue 组件用于选择文件并将其封装到 FormData 中。
示例代码 template div input typefile classfile-btn hoverPointer accept.xls,.xlsx changechangeExcel($event) / div classbutton2 primary clickclickImport立即导入/div /div /template script setup langts import { ref } from vue import { apiGetDownloadImportFile, apiPostImportData, apiPostImportDataLogPage } from /api/encouragementApi import { useAjax } from /hooks/common let fileValue refany(null) // 存储文件 const changeExcel (e: any) { const files e.target.files if (files.length 0) { return false } else if (!/\.(xls|xlsx)$/.test(files[0].name.toLowerCase())) { console.log(上传格式不正确请上传xls或者xlsx格式) return false } fileValue.value files[0] } // 立即导入 const clickImport () { const formData new FormData() formData.append(file, fileValue.value) // 调接口 useAjax({ apiName: apiPostImportData({ encourageTypeId: 1, file: formData }), success: (res: any) { console.log(res) }, failure: () { console.log(导入失败) } }) } /script