0

我正在尝试从 html 表单中获取此图像并将其存储在数据库中。我不确定我做错了什么。我希望能够将图像保存在数据库的 blob 字段中。查看我的代码。我正在使用烧瓶。这里的问题是,我无法将图像存储在数据库中。

   @app.route('/upload', methods=["POST"])
    def get_images():
        if request.method == "POST":
            file = request.files['imagename']
            file.save(file.filename)

            newFile = file.read()

            c, conn = connection()


            c.execute('''
                         INSERT INTO ImageTable( PHT_Image)
                         VALUES(%s)''',
                      [newFile1])
            conn.commit()
4

1 回答 1

0

我强烈建议将文件保存在磁盘上,并将该文件的字符串路径保存到数据库中。例如:

APP_IMAGES = os.path.join(APP_ROOT, 'images/')

class RegistrationForm(Form):
    myImage = FileField('Choose Image file')

@app.route('/register/', methods=["GET","POST"])
def register_page():
    try:
        form = RegistrationForm(CombinedMultiDict((request.files, request.form)))
        filename = 'None'
        if request.method == "POST"):
            filename = secure_filename(form.avatar.data.filename)
            form.myImage.data.save(APP_IMAGES + filename)
            dbu = DatabaseUtility(db)
            dbu.RunCommand("INSERT INTO users (avatar) VALUES (%s)",thwart(filename)))
                dbu.Commit()
                dbu.Disconnect()
                flash("Done!")

    except Exception as e:
        return(str(e))
于 2018-02-26T05:38:09.823 回答