简体   繁体   中英

excel download with Flask-RestPlus?

How to implement an API endpoint to download excel file using Flask-RestPlus?

Previously I had implemented similar function using Pyramid. However that method didn't work here. Here is the old code snippet:

workBook = openpyxl.Workbook()
fileName = 'Report.xls'
response = Response(content_type='application/vnd.ms-excel',
                            content_disposition='attachment; filename=%s' % fileName)
workBook.save(response)
return response

Thanks for the help.

send_from_directory provides a secure way to quickly expose static files from an upload folder or something similar when using Flask-RestPlus

from flask import send_from_directory
import os

@api.route('/download')
class Download(Resource):
    def get(self):
        fileName = 'Report.xls'
        return send_from_directory(os.getcwd(), fileName, as_attachment=True)

I have assumed file is in current working directory. The path to download file can be adjusted accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM