如何使用 flask 下载 PDF
How do I download a PDF with flask
我正在尝试使用 flask 生成 pdf 文件下载,这是我的代码:
@app.route('/return-files/')
def return_files_tut():
from flask import send_file
with open(os.path.join(app.config['FACTURAS_FOLDER'],'Transferencia_a_otras_cuentas_1.pdf'), 'rb') as static_file:
return send_file(static_file, attachment_filename='file.pdf')
但是我有以下缺点
[2021-06-21 23:34:44 +0000] [12] [ERROR] Error handling request /return-files/
plan-management | Traceback (most recent call last):
plan-management | File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/sync.py", line 136, in handle
plan-management | self.handle_request(listener, req, client, addr)
plan-management | File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/sync.py", line 182, in handle_request
plan-management | resp.write_file(respiter)
plan-management | File "/usr/local/lib/python3.9/site-packages/gunicorn/http/wsgi.py", line 385, in write_file
plan-management | if not self.sendfile(respiter):
plan-management | File "/usr/local/lib/python3.9/site-packages/gunicorn/http/wsgi.py", line 355, in sendfile
plan-management | if not util.has_fileno(respiter.filelike):
plan-management | File "/usr/local/lib/python3.9/site-packages/gunicorn/util.py", line 574, in has_fileno
plan-management | obj.fileno()
plan-management | ValueError: I/O operation on closed file
无需实际打开文件,直接发送文件即可...
请注意,此代码未经测试,但应该可以工作™
此外,我不会将导入放在函数本身中,因为每次执行函数时它都会导入。
from flask import send_file
@app.route('/return-files/')
def return_files_tut():
filename = os.path.join(app.config['FACTURAS_FOLDER'],'Transferencia_a_otras_cuentas_1.pdf')
return send_file(filename, attachment_filename='file.pdf', as_attachment=True)
我正在尝试使用 flask 生成 pdf 文件下载,这是我的代码:
@app.route('/return-files/')
def return_files_tut():
from flask import send_file
with open(os.path.join(app.config['FACTURAS_FOLDER'],'Transferencia_a_otras_cuentas_1.pdf'), 'rb') as static_file:
return send_file(static_file, attachment_filename='file.pdf')
但是我有以下缺点
[2021-06-21 23:34:44 +0000] [12] [ERROR] Error handling request /return-files/
plan-management | Traceback (most recent call last):
plan-management | File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/sync.py", line 136, in handle
plan-management | self.handle_request(listener, req, client, addr)
plan-management | File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/sync.py", line 182, in handle_request
plan-management | resp.write_file(respiter)
plan-management | File "/usr/local/lib/python3.9/site-packages/gunicorn/http/wsgi.py", line 385, in write_file
plan-management | if not self.sendfile(respiter):
plan-management | File "/usr/local/lib/python3.9/site-packages/gunicorn/http/wsgi.py", line 355, in sendfile
plan-management | if not util.has_fileno(respiter.filelike):
plan-management | File "/usr/local/lib/python3.9/site-packages/gunicorn/util.py", line 574, in has_fileno
plan-management | obj.fileno()
plan-management | ValueError: I/O operation on closed file
无需实际打开文件,直接发送文件即可...
请注意,此代码未经测试,但应该可以工作™
此外,我不会将导入放在函数本身中,因为每次执行函数时它都会导入。
from flask import send_file
@app.route('/return-files/')
def return_files_tut():
filename = os.path.join(app.config['FACTURAS_FOLDER'],'Transferencia_a_otras_cuentas_1.pdf')
return send_file(filename, attachment_filename='file.pdf', as_attachment=True)