Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

577
Views
¿Cómo devolver el archivo xlsx de la memoria en fastapi?

Quiero dar xlsx a pedido. Con el uso BytesIO y xlsxwriter , creo un archivo.

Usando el siguiente código, puedo descargar un archivo .txt vacío (!):

 @router.get("/payments/xlsx", response_description='xlsx') async def payments(): """sss""" output = BytesIO() workbook = xlsxwriter.Workbook(output) worksheet = workbook.add_worksheet() worksheet.write(0, 0, 'ISBN') worksheet.write(0, 1, 'Name') worksheet.write(0, 2, 'Takedown date') worksheet.write(0, 3, 'Last updated') workbook.close() output.seek(0) return StreamingResponse(output)

Si agrego headers={'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'} este error en el navegador:

 Unable to open file You may be having a problem connecting with the server, or the file that you wanted to open was corrupted.

¿Cómo puedo arreglar esto?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Tienes que establecer el encabezado Content-Disposition en la respuesta.

 @router.get("/payments/xlsx", response_description='xlsx') async def payments(): output = BytesIO() workbook = xlsxwriter.Workbook(output) worksheet = workbook.add_worksheet() worksheet.write(0, 0, 'ISBN') worksheet.write(0, 1, 'Name') worksheet.write(0, 2, 'Takedown date') worksheet.write(0, 3, 'Last updated') workbook.close() output.seek(0) headers = { 'Content-Disposition': 'attachment; filename="filename.xlsx"' } return StreamingResponse(output, headers=headers )
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!