Python 调用“返回文本文档而不是图像”时发送_file()
我想从服务器端向客户端发送一个映像文件。我正在使用flask框架 但问题是,每当我调用Python 调用“返回文本文档而不是图像”时发送_file(),python,web,flask,Python,Web,Flask,我想从服务器端向客户端发送一个映像文件。我正在使用flask框架 但问题是,每当我调用send\u file()所在的路由时,返回的响应都是一个文件。当我单击此文件时,gedit将打开该文件,但该文件中没有任何内容。这意味着它必须是文本文件 我查阅了烧瓶文件 以下是我在代码中所做的操作: @app.route('/try') def trial(): todown = 'https://igcdn-photos-e-a.akamaihd.net//hphotos-ak-xaf1//t51
send\u file()
所在的路由时,返回的响应都是一个文件。当我单击此文件时,gedit
将打开该文件,但该文件中没有任何内容。这意味着它必须是文本文件
我查阅了烧瓶文件
以下是我在代码中所做的操作:
@app.route('/try')
def trial():
todown = 'https://igcdn-photos-e-a.akamaihd.net//hphotos-ak-xaf1//t51.2885-15//e35//12093691_1082288621781484_1524190206_n.jpg'
resp = requests.get(todown)
return send_file(resp,mimetype="image/jpeg",attachment_filename="img.jpg",as_attachment=True)
每当我加载localhost:5000/try
时,都会下载一个文件,但不会下载我要下载的图像文件
我在终端中遇到的错误是AttributeError:'Response'对象没有属性'read'错误
一定是什么问题。上面的片段中缺少什么
resp
是一个requests.models.Response
对象,不是字符串也不是字节:
>>> import requests
>>> todown = 'https://igcdn-photos-e-a.akamaihd.net//hphotos-ak-xaf1//t51.2885-15//e35//12093691_1082288621781484_1524190206_n.jpg'
>>> resp = requests.get(todown)
>>> resp
<Response [200]>
>>> type(resp)
<class 'requests.models.Response'>
请查看更多详细信息
然后,因为
Flask.send_file()
发送一个文件,所以在发送之前需要将图像写入一个文件
但由于您不需要在服务器上使用此图像,因此我建议在这种情况下使用,这样您就不需要在发送图像后删除该图像。请注意,如果要发送文本文件,请使用
例如:
import requests
from io import BytesIO
from flask import Flask, send_file
app = Flask(__name__)
@app.route('/')
def tria():
todown = 'https://igcdn-photos-e-a.akamaihd.net//hphotos-ak-xaf1//t51.2885-15//e35//12093691_1082288621781484_1524190206_n.jpg'
resp = requests.get(todown)
return send_file(BytesIO(resp.content), mimetype="image/jpeg", attachment_filename="img2.jpg", as_attachment=True)
app.run(port=80, debug=True)
import tempfile
import requests
from flask import Flask, send_file
app = Flask(__name__)
@app.route('/')
def tria():
todown = 'https://igcdn-photos-e-a.akamaihd.net//hphotos-ak-xaf1//t51.2885-15//e35//12093691_1082288621781484_1524190206_n.jpg'
resp = requests.get(todown)
with tempfile.NamedTemporaryFile() as f:
# create a file-like object use `NamedTemporaryFile()` and `with`
# as the basic usage which said in the document
f.write(resp.content)
# write the content of the image into it
return send_file(f.name, mimetype="image/jpeg",
attachment_filename="img2.jpg", as_attachment=True)
# `f.name` is the temp file's filename
app.run(port=80, debug=True)
但是,如果您想将图像写入文件并发送,请确保您也可以这样做。我们可以使用创建临时文件,而不只是创建文件,以避免重写重要文件 从文件中: 此函数的操作与
TemporaryFile()
的操作完全相同,只是保证文件在文件系统中有一个可见的名称(在Unix上,目录项没有取消链接)
可以从文件对象的name
属性中检索该名称。在命名的临时文件仍处于打开状态时,该名称是否可用于再次打开该文件,因平台而异(在Unix上可以使用该名称;在Windows NT或更高版本上不能使用该名称)。如果delete为true(默认值),则文件一关闭即被删除
返回的对象始终是一个类似文件的对象,其file
属性是底层的true file对象。这个类似文件的对象可以在with
语句中使用,就像普通文件一样
例如:
import requests
from io import BytesIO
from flask import Flask, send_file
app = Flask(__name__)
@app.route('/')
def tria():
todown = 'https://igcdn-photos-e-a.akamaihd.net//hphotos-ak-xaf1//t51.2885-15//e35//12093691_1082288621781484_1524190206_n.jpg'
resp = requests.get(todown)
return send_file(BytesIO(resp.content), mimetype="image/jpeg", attachment_filename="img2.jpg", as_attachment=True)
app.run(port=80, debug=True)
import tempfile
import requests
from flask import Flask, send_file
app = Flask(__name__)
@app.route('/')
def tria():
todown = 'https://igcdn-photos-e-a.akamaihd.net//hphotos-ak-xaf1//t51.2885-15//e35//12093691_1082288621781484_1524190206_n.jpg'
resp = requests.get(todown)
with tempfile.NamedTemporaryFile() as f:
# create a file-like object use `NamedTemporaryFile()` and `with`
# as the basic usage which said in the document
f.write(resp.content)
# write the content of the image into it
return send_file(f.name, mimetype="image/jpeg",
attachment_filename="img2.jpg", as_attachment=True)
# `f.name` is the temp file's filename
app.run(port=80, debug=True)
您没有得到
AttributeError:“Response”对象没有属性“read”
错误吗?您是否需要使用send\u文件(响应内容…
?@SimonMᶜKenzie如果我使用相应的内容
同样的事情会发生,你会像我一样解释我。很好的解释。谢谢you@SurajPalwe:没问题,很高兴能帮上忙:)我想改变两种解决方案的顺序。首先将图像写入文件(您也需要在某个时候将其删除…)很少是一个好主意,尤其是使用硬编码的文件名而不是由tempfile
模块创建的文件。这只是在一次处理多个请求时自找麻烦。更好,但我会将此用于文件而不是散列:是的,避免磁盘IO总是有意义的,特别是对于像典型图像文件这样小的文件。如果你想为它服务很多次,这将是一个不同的事情,但你会使用更持久的东西无论如何。。。