简体   繁体   English

使用Flask-RestPlus进行excel下载?

[英]excel download with Flask-RestPlus?

How to implement an API endpoint to download excel file using Flask-RestPlus? 如何使用Flask-RestPlus实现API端点下载excel文件?

Previously I had implemented similar function using Pyramid. 以前我使用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 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)

I have assumed file is in current working directory. 我假设文件在当前工作目录中。 The path to download file can be adjusted accordingly. 可以相应地调整下载文件的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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