26

我正在使用flask,并尝试使用快速入门教程做一些非常简单的事情,只是在我的机器(本地服务器)上运行。我制作了一个简单的上传表单,它成功上传了一个图像文件。然后我想将此图像作为变量传递给 atemplate.html以在页面内显示。该template.html文件显示正常,但图像始终是broken link image symbol. 我尝试了许多不同的路径,但我觉得我做的事情有点不对劲。

import os
from flask import Flask, request, redirect, url_for, send_from_directory, 
                  render_template

UPLOAD_FOLDER = '/home/me/Desktop/projects/flask/uploads'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

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

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files['file']
        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 '''
    <!doctype html>
    <title>Upload new File</title>
    <h1>Upload new File</h1>
    <form action="" method=post enctype=multipart/form-data>
      <p><input type=file name=file>
         <input type=submit value=Upload>
    </form>
    '''

@app.route('/uploads/<filename>')
def uploaded_file(filename):
    filename = 'http://127.0.0.1:5000/uploads/' + filename
    return render_template('template.html', filename = filename)

if __name__ == '__main__':
    app.run()

这是template.html

<!doctype html>
<title>Hello from Flask</title>
{% if filename %}
  <h1>some text<img src="{{filename}}"> more text!</h1>
{% else %}
  <h1>no image for whatever reason</h1>
{% endif %}

如何将上传的图像文件传递给template.html它以正确显示?

谢谢

4

2 回答 2

27

现在发生的是/uploads/foo.jpg返回 template.html 中的 HTML。在那里,您尝试将其/uploads/foo.jpg用作 img 标签的来源。无处可提供实际图像。

让我们像这样修改它:/show/foo.jpg返回 HTML 页面并/uploads/foo.jpg返回图像。用这两条替换后一条路线,你应该很高兴:

@app.route('/show/<filename>')
def uploaded_file(filename):
    filename = 'http://127.0.0.1:5000/uploads/' + filename
    return render_template('template.html', filename=filename)

@app.route('/uploads/<filename>')
def send_file(filename):
    return send_from_directory(UPLOAD_FOLDER, filename)
于 2012-06-29T13:32:45.687 回答
12

从 upload_file 函数中,我们前往 template.html 并且将被重定向回来<img src="{{ url_for('send_file', filename=filename) }}">,我们点击 send_file 函数,该函数将显示模板内的 HTML 内容以及上传并存储在指定的 UPLOAD_FOLDER 中的图像。from werkzeug import secure_filename你在 py 文件中也缺少这个

@app.route('/show/<filename>')
def uploaded_file(filename):
    return render_template('template.html', filename=filename)

@app.route('/uploads/<filename>')
def send_file(filename):
    return send_from_directory(UPLOAD_FOLDER, filename)

现在你的 template.html 看起来像这样..

<!doctype html>
<title>Hello from Flask</title>
{% if filename %}
  <h1>some text <img src="{{ url_for('send_file', filename=filename) }}">more text!</h1>
{% else %}
  <h1>no image for whatever reason</h1>
{% endif %}
于 2013-10-02T22:29:07.467 回答