簡體   English   中英

使用Flask-RestPlus進行excel下載?

[英]excel download with Flask-RestPlus?

如何使用Flask-RestPlus實現API端點下載excel文件?

以前我使用Pyramid實現了類似的功能。 但是這種方法在這里不起作用。 這是舊的代碼片段:

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)

我假設文件在當前工作目錄中。 可以相應地調整下載文件的路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM