我在加载Flask图片时遇到问题

时间:2018-09-07 20:05:09

标签: python flask

我在项目中上传了用于相册的Uploading Files文档。 İmage上传不起作用。

Python代码:

UPLOAD_FOLDER = '/static/uploads'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])

app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

#Album form
def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route("/admin/fotografAlbumu", methods=["GET", "POST"])
@login_required
def upload():
    if request.method == 'POST':
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['photo']
        # if user does not select file, browser also
        # submit a empty part without filename
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            import pdb;pdb.set_trace()
            return redirect(url_for('uploaded_file', filename=filename))
    return render_template('/admin/galeri.html')

HTML代码:

<form method="post" class="col-12" enctype="multipart/form-data">
                 <ul class="row">
                     <li class="col-xs-6 col-lg-12" style="border: none;">
                            <div class="form-group">
                                    <input id="input-id" type="file" name="photo" class="file form-control" multiple data-preview-file-type="text" data-allowed-file-extensions='["png", "jpg"]'>
                             </div>
                     </li>
                 </ul>
                 <button type="submit" class="btn form-control btn-default">Kaydet</button>
                </form>

输出:当我在页面上选择图像并上传它们时,它们将重定向到同一页面,并且不会加载图像。我需要做什么?我正在工作2-3天,但找不到解决方案。

1 个答案:

答案 0 :(得分:0)

我花了几分钟使它工作,然后您就可以了:

python:

import os
from flask import Flask, request, render_template, send_from_directory, redirect, url_for
from werkzeug.utils import secure_filename

app = Flask(__name__)

UPLOAD_FOLDER = 'static/uploads'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])

app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

#Album form
def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route("/photo", methods=["GET", "POST"])
def upload():
    if request.method == 'POST':
        if 'photo' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['photo']
        # if user does not select file, browser also
        # submit a empty part without filename
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('uploaded_file', filename=filename))
    return render_template('/add_photo.html')

@app.route('/uploaded_file/<path:filename>')
def uploaded_file(filename):
    print(filename)
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

html:

 <form method="post" class="col-12" enctype="multipart/form-data">
 <ul class="row">
   <li class="col-xs-6 col-lg-12" style="border: none;">
     <div class="form-group">
       <input id="input-id" type="file" name="photo" class="file form-control" multiple data-preview-file-type="text" data-allowed-file-extensions='["png", "jpg"]'>
     </div>
   </li>
 </ul>
 <button type="submit" class="btn form-control btn-default">Kaydet</button>
</form>

主要问题是您试图从表单中删除file,但是表单中只有一个名为photo的字段。