0

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

1 回答 1

0
photos = UploadSet("photos", IMAGES)

此行控制只能上传图片文件。但是,如果您想更具体,您可以

photos= UploadSet('photos', ('png', 'jpg'))
于 2018-07-18T10:52:58.783 回答