如何使用flask检索从MySQL数据库保存为BLOB类型的选定图像

时间:2017-11-10 09:52:03

标签: python mysql flask

我创建了一个包含候选人名称和图像的表格,我在表格中插入了值。我的问题是如何从数据库中检索存储的图像并在网页中查看?是否可以检索所有存储的图像?对此有任何帮助将非常感谢谢谢。

这是我的py

import os
from flask import Flask,request,render_template
from flaskext.mysql import MySQL
mysql=MySQL()

app=Flask(__name__)
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
app.config['MYSQL_DATABASE_DB'] = 'matrimony'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
APP_ROOT=os.path.dirname(os.path.abspath(__file__))


@app.route('/',methods=['GET','POST'])
def get_data():
  if request.method=='POST':
    print("hlo")
    candidate_name=request.form['cname']
    file=request.files['canpic']

    conn = mysql.connect()



    target = os.path.join(APP_ROOT, 'file')
    if not os.path.isdir(target):
        os.makedirs(target)
    print (target)
    filename = file.filename
    destination = "/".join([target, filename])
    print (destination)
    file.save(destination)
    print "saved"
    datafile = open(destination, "rb")
    print "file opend"
    d = open("F:/five pro/testvote/test.jpg", "wb")
    print "file test opnend"
    thedata = datafile.read()
    print "file readed"
    d.write(thedata)
    d.close()
    datafile.close()

    b1 = open("F:/five pro/testvote/test.jpg", 'rb').read()
    target1 = os.path.join(APP_ROOT, 'file')


    conn = mysql.connect()
    cursor = conn.cursor()

    print ("yoyo")
    cursor.execute("insert into Votetable2  values (%s,%s)", (candidate_name,b1))

    print "execuet"

    conn.commit()
  return render_template('final_pic.html')

if __name__=='__main__':
    app.run(debug=True)

final_pic.html:

<!DOCTYPE html>
  <html>
  <body>

  <form method="POST" action="/" enctype="multipart/form-data">

  Candidate Name: <input type="text" name="cname"><br>
      Candidate Image<input type="file" name="canpic" accept="image/*"><br>



 <input type="submit" value="Submit">

 </form>

</body>
</html>

这是我的另一个HTML,我试图使用select candidate_name, candidate_image from votetable2

从数据库中检索所选图像

retrieve.html

<!DOCTYPE html>
<html lang="en">


<body><form>

             <table border="2"><tr>
                <td>CANDIDATE NAME</td>
                 <td>CANDIDATE IMAGE</td>
                </tr>

                {% for row in a %}

                    <tr>
                        <td>{{ row[0] }}<input type="submit" value="Done"></td>
                        <td>{{ row[1] }}

                  </tr>
                {% endfor %}
            </table>
        </form>
</body></html>

1 个答案:

答案 0 :(得分:2)

是。可以检索存储在数据库中的所有图像。您可以使用IO检索图像。

cursor.execute("select picture,voterid from registration")
data = cursor.fetchall()
for row in dataw:
    file_like = io.BytesIO(row[0])
    file = PIL.Image.open(file_like)
    target = os.path.join("/path-to-save/", 'folder-save')
    if not os.path.isdir(target):
       os.makedirs(target)
    destination = "/".join([target, file.filename])
    file.save(destination)

在html中,您可以通过将图像保存在静态文件夹中并将图像名称传递给html来查看图像。这里的image_name是从flask传递到html的值

<img id="User_image" src="{{ url_for('static',filename=image_name) }}" style="width:100px;border-radius:50%;">