我要上传带有dropzone(flask)的图片,并在上传带有该图片的显示页面后(根据请求,我不想像此示例一样将图片保存在磁盘上)和文件名。我在github https://github.com/greyli/flask-dropzone/blob/master/examples/in-form/app.py
上找到了示例from flask import Flask, render_template, request
from flask_dropzone import Dropzone
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config.update(
UPLOADED_PATH=os.path.join(basedir, 'uploads'),
# Flask-Dropzone config:
DROPZONE_ALLOWED_FILE_TYPE='image',
DROPZONE_MAX_FILE_SIZE=3,
DROPZONE_MAX_FILES=1,
DROPZONE_IN_FORM=True,
DROPZONE_UPLOAD_ON_CLICK=True,
DROPZONE_UPLOAD_ACTION='handle_upload', # URL or endpoint
DROPZONE_UPLOAD_BTN_ID='submit',
)
dropzone = Dropzone(app)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def handle_upload():
for key, f in request.files.items():
if key.startswith('file'):
print( f.filename)
return '', 204
@app.route('/form', methods=['POST'])
def handle_form():
############################################
#i want to access request.files.items() there
#############################################
title = request.form.get('title')
description = request.form.get('description')
return 'file uploaded and form submit<br>title: %s<br> description: %s' % (title, description)
if __name__ == '__main__':
app.run(debug=True)
在handle_upload()中上传图像后,我可以从请求中获取图像,但是要在handle_form()中渲染模板,我需要将图像保存到磁盘。我想将图像从handle_upload()发送到handle_form()(不使用全局变量)
答案 0 :(得分:0)
如果图像是通过 handle_upload()发送的,则无法从 handle_form()的请求中获取图像文件。 See Lifetime of Request Context
您有2种选择: