I created a table with name and image of the candidate,I inserted the values in the table. My question is how can I retrieve the stored image from the database and view in webpage? Is that possible to retrieve all the stored images?Any help with this would be much grateful Thank you.
Here is my 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>
and here is my another HTML where I am trying to retrieve the selected image from the database using 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>