我试图将图像上传到系统上的文件夹并为其提供唯一标识符。这样每个成员都可以拥有自己的个人资料图片。我在分配ID时遇到问题,并且不确定我是否正确地进行了此操作。另外,将图像放在SQLalchemy数据库中还是只放在文件夹中会更好吗?
@main.route("/upload", methods=['GET', 'POST'])
@login_required
def upload():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
rec = file(filename=filename, user=g.user.id)
rec.store()
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('.home'))
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>
<p>%s</p>
""" % "<br>".join(os.listdir(app.config['UPLOAD_FOLDER'],))
我的错误: 文件&#34; /home/ed/Development/Python/social/app/main/views.py",第253行,上传 rec = file(filename = filename,user = g.user.id) TypeError:&#39; FileStorage&#39;对象不可调用
答案 0 :(得分:2)
编写文件的正确方法是:
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'], actual_filename))
要使用此功能,您的配置中应该有UPLOAD_FOLDER
。