如何使用 Flask-RestPlus 实现 API 端点以下载 excel 文件?
以前我使用金字塔实现了类似的功能。但是,该方法在这里不起作用。这是旧的代码片段:
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
感谢您的帮助。
send_from_directory提供了一种安全的方法,可以在使用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)
我假设文件在当前工作目录中。下载文件的路径可以相应地调整。