Python 如何将'qrcode.make'的结果传递给Flask';s'send_file',而不先将文件保存到磁盘?
目前我正在使用flask中的“send_file”,但它会读取打开文件的路径。我已经加载了该文件并希望返回它,尽管我找不到用于该文件的函数。有什么想法吗 例如:Python 如何将'qrcode.make'的结果传递给Flask';s'send_file',而不先将文件保存到磁盘?,python,api,file,flask,Python,Api,File,Flask,目前我正在使用flask中的“send_file”,但它会读取打开文件的路径。我已经加载了该文件并希望返回它,尽管我找不到用于该文件的函数。有什么想法吗 例如: return send_file(filename_or_fp="../file.jpg", mimetype='image/jpeg') 我想这样退: file = qrcode.make("sample text") return send_file(filename_or_fp=file, mimetype='image/jpe
return send_file(filename_or_fp="../file.jpg", mimetype='image/jpeg')
我想这样退:
file = qrcode.make("sample text")
return send_file(filename_or_fp=file, mimetype='image/jpeg')
send_files返回以下错误:
AttributeError:“Image”对象没有属性“read”
您需要将文件
保存到字节IO
缓冲区,然后将其传递到send_file
函数
此外,您不需要编写filename\u或\u fp=
,因为这是一个位置参数
该应用程序应该如下所示:
import qrcode
from flask import Flask, send_file
import io
app = Flask(__name__)
@app.route('/')
def index():
file = qrcode.make('sample text')
buf = io.BytesIO()
file.save(buf)
buf.seek(0)
return send_file(buf, mimetype='image/jpeg')
请显示“加载”文件的代码。更新的问题。。