我目前有一个Flask路线,通过POST显示文件内容,
并将其存储在文件系统上,例如:
@app.route('/upload', methods=['POST'])
def upload_file():
def allowed_file(f):
return True
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(upload_dir(), filename))
return "", 200
我想将它存储在postgres的BYTEA列中,我不知道如何将“data”参数绑定到insert语句
db.session.execute("""
INSERT INTO uploaded_file(id, name, type, data)
VALUES (:id, :name, :type, %(:data)s)""",
{"id": str(id),"name": file.filename,"type": "...","data": ???}
解决方法:
request.files中的对象是FileStorage个对象.他们采用与normal file objects in python相同的方法.
因此,要将文件的内容作为二进制文件,请执行以下操作:
data = request.files['file'].read()
然后将该参数传递给INSERT.