简体   繁体   English

上传 Flask 中的图像并将其存储在本地文件夹中

[英]Upload image in Flask and store it in local folder

I want to upload an image and pass it to the python code in flask so it can store and call the image locally.我想上传一个图像并将其传递给 flask 中的 python 代码,以便它可以在本地存储和调用图像。 From a tutorial I have this code, however there seems to be a problem with the query, the request always ends with 'No file part':从教程中我有这段代码,但是查询似乎有问题,请求总是以“无文件部分”结尾:

if 'file' not in request.files:
    flash('No file part')
    return redirect(request.url)

If I check which data is in the request:如果我检查请求中包含哪些数据:

print(request.data)
print(request.args)
print(request.form) 

that gives me the following results:这给了我以下结果:

b''
ImmutableMultiDict([])
ImmutableMultiDict([('file', '6b6e63084d829d765f318a123f9997d6.jpg')])

Here is my python code这是我的 python 代码

ALLOWED_IMAGE_TYPES = set(["png", "jpg", "jpeg", "gif"])

def allowed_file(filename):
    print("Check if image types is allowed")
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_IMAGE_TYPES


@app.route("/crop", methods=["GET", "POST"])
@login_required
def crop():

    if request.method == "POST":
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        if file.filename == '':
            flash('No image selected for uploading')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            print("test2")
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            print('upload_image filename: ' + filename)
            flash('Image successfully uploaded and displayed below')
            return render_template('index.html', filename=filename)
        else:
            flash('Allowed image types are - png, jpg, jpeg, gif')
            return redirect(request.url)

    else:
        return render_template("crop.html")

@app.route('/display/<filename>')
def display_image(filename):
    print('display_image filename: ' + filename)
    return redirect(url_for('static', filename='uploads/' + filename), code=301)
<div class="container">
<div class="row">
<p>
    {% with messages = get_flashed_messages() %}
      {% if messages %}
        <ul>
        {% for message in messages %}
          <li>{{ message }}</li>
        {% endfor %}
        </ul>
      {% endif %}
    {% endwith %}
</p>
{% if filename %}
    <div>
        <img src="{{ url_for('display_image', filename=filename) }}">
    </div>
{% endif %}
<form method="POST" action="/crop" enctype="multipart/form-data">
    <dl>
        <p>
            <input type="file" name="file" value="file" id="file" class="form-control" autocomplete="off" required>
        </p>
    </dl>
    <p>
        <input type="submit" value="Submit" class="btn btn-info">
    </p>
</form>

request.files is a dict object, so checking that the name is in it should work. request.files是一个字典 object,因此检查名称是否在其中应该可以工作。 For some reason it's not, so you could try checking if it's in keys() instead, although that should be unnecessary.由于某种原因它不是,所以你可以尝试检查它是否在keys()中,尽管这应该是不必要的。

if 'file' not in request.files.keys():
    flash('No file part')
    return redirect(request.url)

Found the problem in the HTML code, the quotation marks around the curly brackets were wrong.在HTML代码中发现问题,大括号两边的引号是错误的。

 <img src={{ url_for('display_image', filename=filename) }}>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM