Flask-uploads定义允许哪些文件

时间:2018-01-21 18:58:28

标签: python flask flask-uploads

我正在使用名为" 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)

这是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)

1 个答案:

答案 0 :(得分:0)

photos = UploadSet("photos", IMAGES)

此行控制只能上载图像文件。 但是,如果您想更具体一点,可以

photos= UploadSet('photos', ('png', 'jpg'))