Comments (3)
跨域问题应该是让后端去解决才是。
from vue3-antdv-admin.
老哥,你看我的代码,
@get('export-xlsx')
@ApiOperation({ summary: '导出excel' })
@ApiResult({ type: [DeviceDataEntity], isPage: true })
async exportDeviceDataXlsx(@res() res: FastifyReply, @query() dto: DeviceDataQueryDto) {
const dataEntities = await this.deviceDataService.findAllByExport(dto);
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet('My Sheet');
// 添加表头
worksheet.columns = [
{ header: 'ID', key: 'id', width: 10 },
{ header: '设备序列号', key: 'product_sn', width: 20 },
{ header: '模型名称', key: 'model_name', width: 20 },
{ header: '样本编号', key: 'sample_no', width: 20 },
{ header: '取样时间', key: 'sampleTimestamp', width: 20 },
{ header: '设备名称', key: 'deviceName', width: 20 },
{ header: '设备型号', key: 'deviceModel', width: 20 },
{ header: '设备位置', key: 'devicePosition', width: 20 },
{ header: '解析结果', key: 'parsedResult', width: 40 },
];
// 添加数据
dataEntities.forEach(item => {
worksheet.addRow({
id: item.id,
product_sn: item.product_sn,
model_name: item.model_name,
sample_no: item.sample_no,
sampleTimestamp: item.sampleTimestamp,
deviceName: item.deviceId.deviceName,
deviceModel: item.deviceId.deviceModel,
devicePosition: item.deviceId.devicePosition,
parsedResult: item.parsedResult,
});
});
// 修正:使用 Fastify 的方式设置响应头
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,POST');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.header('Content-Disposition', 'attachment; filename=chat.xlsx');
console.log(res.getHeaders())
// 修正:使用 Fastify 的方式发送文件
await workbook.xlsx.write(res.raw); // 注意:这里使用 res.raw 来直接操作 Node.js 原生的响应流
res.send(); // 在 Fastify 中,通常不需要显式调用 .end(),使用 .send() 即可
}
就这个接口前端请求一直报跨域错误,如果可以,方便加您个联系方式沟通吗?
from vue3-antdv-admin.
改用 Header
装饰器试试
@get('export-xlsx')
@Header('Access-Control-Allow-Origin', '*')
@ApiOperation({ summary: '导出excel' })
@ApiResult({ type: [DeviceDataEntity], isPage: true })
async exportDeviceDataXlsx(@res() res: FastifyReply, @query() dto: DeviceDataQueryDto) {}
from vue3-antdv-admin.
Related Issues (20)
- 找不到模块“vue-router”。你的意思是要将 "moduleResolution" 选项设置为 "node",还是要将别名添加到 "paths" 选项中?
- Table的动态列怎么设置?
- 不能使用unplugin-auto-import这个插件吗? HOT 1
- 页面锁屏功能探讨🙏 HOT 1
- Date rank filter HOT 1
- How to use BasicForm? HOT 1
- 代理地址访问不了了? HOT 1
- 请问 public/iconfont.js 中有哪些图标,如何查看? HOT 1
- 您NodeJs是用哪个版本的?我npm install还跑不起来 HOT 3
- 注释 demos 路由后会报错 HOT 2
- 大概什么时候趋于稳定,想用于生产 HOT 1
- feature: 新功能,请问有计划对 H5 适配计划吗? HOT 2
- 期待电商 sku 功能
- Node 18.17, pnpm dev ,运行出错error when starting dev server: Error: read ECONNRESET at AxiosError.from HOT 2
- 部署到服务器,直接报错了,不知道是否可以指导一下? HOT 15
- 菜单BUG
- 请问当前是否支持 eslint autofix onsave? HOT 1
- 退出功能有点小问题,没有退出到登录页面 HOT 1
- 弹窗内容太长了,看不到提交和取消按钮。只能拖动弹窗页面才能显示出现 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vue3-antdv-admin.