我正在使用名为" flask -uploads“的flask扩展,我只想允许上传真实的图片(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)flask-uploads的配置如下:
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)发布于 2018-07-18 18:52:59
photos = UploadSet("photos", IMAGES)此行控制只能上传图像文件。但是,如果您希望更具体,您可以
photos= UploadSet('photos', ('png', 'jpg'))https://stackoverflow.com/questions/48370533
复制相似问题