Ris*_*hiC 2 delete-file sendfile flask python-3.x
我有一个 Flask 后端,它根据一些用户输入生成图像,并使用send_file()Flask 的功能将该图像发送到客户端。
这是Python服务器代码:
@app.route('/image',methods=['POST'])
def generate_image():
cont = request.get_json()
t=cont['text']
print(cont['text'])
name = pic.create_image(t) //A different function which generates the image
time.sleep(0.5)
return send_file(f"{name}.png",as_attachment=True,mimetype="image/png")
Run Code Online (Sandbox Code Playgroud)
我想在将该图像发送到客户端后从服务器中删除它。
我该如何实现它?
好吧,我解决了。我使用了@app.after_requestif 条件来检查端点,然后删除了图像
@app.after_request
def delete_image(response):
global image_name
if request.endpoint=="generate_image": //this is the endpoint at which the image gets generated
os.remove(image_name)
return response
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2494 次 |
| 最近记录: |