如何将使用烧瓶dropzone上传的图像传递到另一页

时间:2019-04-14 22:51:05

标签: python html5 flask file-upload dropzone.js

因此,我已将图像上传到带有flask dropzone的uploads文件夹中,并且希望能够将其传递到我的makeMeme页面,以便随后可以使用html img标签显示该图像。这是我上传图片并尝试通过图片的方法。但是,当我运行基本的img并尝试引用文件时,出现错误。 这是我得到的属性错误:

window.screen

这是我的主要python页面。

resize

1 个答案:

答案 0 :(得分:0)

首先创建templatesstatic文件夹
然后在模板内复制upload.html和makeMeme.html
然后执行app.py
希望这能解决您的问题

app.py

import os
from flask import Flask, render_template,request, url_for
from flask_dropzone import Dropzone

app = Flask(__name__)
app.secret_key = 'key'

dir_path = os.path.dirname(os.path.realpath(__file__))


app.config.update(
    UPLOADED_PATH=os.path.join(dir_path, 'static'),
    # Flask-Dropzone config:
    DROPZONE_ALLOWED_FILE_TYPE='image',
    DROPZONE_MAX_FILE_SIZE=3,
    DROPZONE_MAX_FILES=20
)
app.config['DROPZONE_REDIRECT_VIEW'] = 'makeMeme'

dropzone = Dropzone(app)

filename = None

@app.route('/upload', methods=['POST', 'GET'])
def upload():
    global filename
    file = None
    if request.method == 'POST':
        f = request.files.get('file')
        file = f.save(os.path.join(app.config['UPLOADED_PATH'], f.filename))
        filename = f.filename
    return render_template('upload.html')

@app.route('/makeMeme', methods=['POST', 'GET'])
def makeMeme():
    global filename
    return render_template("makeMeme.html", file_name = filename)

app.run(debug=True)

upload.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Flask-Dropzone Demo</title>
  {{ dropzone.load_css() }}
  {{ dropzone.style('border: 2px dashed #0087F7; margin: 10%; min-height: 400px;') }}
</head>
<body>
  {{ dropzone.create(action='upload') }}
  {{ dropzone.load_js() }}
  {{ dropzone.config() }}
</body>
</html>

makeMeme.html

<!DOCTYPE html>
<html>
    <img src="{{url_for('static',filename=file_name)}}" alt="Fail">
</html>