我正在使用名为“flask-uploads”的烧瓶扩展,我只想允许上传真实图片(jpg、png 等)。如何限制上传文件的类型?
@app.route("/post", methods=["GET", "POST"])
@login_required
def post():
# HERE I'M LINKING TO ANOTHER FILE, THIS IS NOT THE ERROR
post = posts.Post(session["user_id"])
if request.method == "POST" and 'photo' in request.files:
# save photo in img folder
try:
file = photos.save(request.files["photo"])
# I THINK THE ERROR IS IN HERE
except UploadNotAllowed:
return redirect(url_for("index"))
else:
path = 'static/' + str(file)
post.upload(path)
return redirect(url_for("index"))
else:
following = post.loadgroups()
return render_template("post.html", groups = following)
这是烧瓶上传的配置:
from flask_uploads import UploadSet, IMAGES, configure_uploads, UploadNotAllowed
# configure flask_upload API
photos = UploadSet("photos", IMAGES)
app.config["UPLOADED_PHOTOS_DEST"] = "static/img"
configure_uploads(app, photos)