Git Product home page Git Product logo

Comments (3)

buqiyuan avatar buqiyuan commented on May 24, 2024

跨域问题应该是让后端去解决才是。

from vue3-antdv-admin.

sxlh0814 avatar sxlh0814 commented on May 24, 2024

老哥,你看我的代码,
@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.

buqiyuan avatar buqiyuan commented on May 24, 2024

改用 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.