我是Flask的新手并尝试加载存储在我的数据库类型Binary中的图像,如下所示:
avatar_local = db.Column(db.LargeBinary(1024*1024))
现在如何在html文件中显示它?
这是我的文件,无法正常工作:
<img class="img-rounded profile-thumbnail" src="{{ user.avatar_local }}">
我收到了错误信息:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)
这是我的上传功能,它运行正常:
@main.route('/upload', methods=['GET', 'POST'])
def upload_avatar():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
current_user.avatar_local = file.read()
db.session.add(current_user)
db.session.commit()
return redirect(url_for('main.user_page', username=current_user.username))
return render_template('upload_avatar.html', user=current_user)