在Pythonanywhere上的flask中使用send_file()的替代方法? [英] Alternative of send_file() in flask on Pythonanywhere?
问题描述
我是python的新手,仍然在学习.我在pythonanwhere上创建了一个小的python 3.6 Flask webapp,发现send_file()在pythonanywhere服务器上不起作用.我正在积极寻找一种替代方法,可以直接在用户计算机上下载excel文件.我也尝试了 Response ,但它没有提供所需的输出.我在线上阅读了很多有关它的内容,发现如果我们在下面进行设置,则send_file可以正常工作
I am new to python and still learning. I created a small python 3.6 Flask webapp on pythonanwhere and found out that send_file() is not working on pythonanywhere servers. I am actively looking for an alternative to download an excel file directly on the user machine. I also tried Response but it is not giving desired output. I read alot about it online and found that the send_file works fine if we set below
wsgi-disable-file-wrapper =真
但是,我不知道在哪里设置此设置,因为我找不到可以更新此行的uWsgi.ini文件.
However, i don't know where to set this as i couldn't find uWsgi.ini file where i could update this line.
下面是我尝试过的方法,但是它们失败了,请帮忙
Below are the methods which i tried but they failed, please help
SEND_FILE()配置: ->>>未运行..
SEND_FILE() configuration: ->>> Not running..
output = BytesIO()
writer = pd.ExcelWriter(output, engine='xlsxwriter')
workbook = writer.book
output.seek(0)
return send_file(output,attachment_filename="testing.xlsx",as_attachment=True)
输出错误:
Output Error:
return environ.get('wsgi.file_wrapper', FileWrapper)(file, buffer_size)
SystemError: <built-in function uwsgi_sendfile> returned a result with an error set
具有响应配置:
With Response configuration:
writer = pd.ExcelWriter("abc.xlsx", engine='xlsxwriter')
return Response(writer,mimetype="text/csv",headers={"Content-disposition":"attachment; filename=myplot.csv"})
输出错误:
Output error:
Error running WSGI application
TypeError: '_XlsxWriter' object is not iterable
File "/home/hridesh1987/.virtualenvs/myproject/lib/python3.6/site-packages/werkzeug/wsgi.py", line 870, in __next__return self._next()
File "/home/hridesh1987/.virtualenvs/myproject/lib/python3.6/site-packages/werkzeug/wrappers.py", line 83, in _iter_encoded
for item in iterable:
推荐答案
我在PythonAnywhere论坛上提出了同样的问题,他们给了我
I raised the same issue on the PythonAnywhere forums and they gave me this response. Kudos to 'glenn' of the PythonAnywhere staff.
复制粘贴:
from io import BytesIO
from flask import Flask, Response
from werkzeug import FileWrapper
app = Flask(__name__)
@app.route('/')
def hello_world():
b = BytesIO(b"blah blah blah")
w = FileWrapper(b)
return Response(w, mimetype="text/plain", direct_passthrough=True)
我为自己的使用略加调整.我通过 Content-Disposition
标头设置文件名.我还必须调整 FileWrapper
导入,并且 data
在我的代码中已经是一个 BytesIO
对象:
I adapted it slightly for my usage. I set the filename via the Content-Disposition
header. I also had to tweak the FileWrapper
import, and data
is already a BytesIO
object in my code:
from flask import Response
from werkzeug.wsgi import FileWrapper
def send_excel_file(data, filename):
# See: https://www.pythonanywhere.com/forums/topic/13570/
file_wrapper = FileWrapper(data)
headers = {
'Content-Disposition': 'attachment; filename="{}"'.format(filename)
}
response = Response(file_wrapper,
mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
direct_passthrough=True,
headers=headers)
return response
这篇关于在Pythonanywhere上的flask中使用send_file()的替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!