Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将'qrcode.make'的结果传递给Flask';s'send_file',而不先将文件保存到磁盘?_Python_Api_File_Flask - Fatal编程技术网

Python 如何将'qrcode.make'的结果传递给Flask';s'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

目前我正在使用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/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')

请显示“加载”文件的代码。更新的问题。。