这是我的第一个问题。
我让用户将自己的图像上传到数据库。该图像存储为 BLOB。
我能够成功地做到这一点。我正在使用 MySQL 作为数据库。
我遇到问题的部分是在调用该 BLOB 时将其显示为网站上的图像。
现在只有二进制数据,许多奇怪的符号正在显示。我认为这是 HTTP 标头的问题。现在它在:
print "Content-Type: text/html"
我试过了:
print "Content-Type: image/jpeg"
我正在使用 Python 连接数据库并编写 HTML。
编辑:代码:
def showFile():
# do SQL to retrieve blob where filename
conn, cursor = getConnectionAndCursor()
sql = """
select data
from upload
where id=1
"""
cursor.execute(sql)
data = cursor.fetchone()
blob = data[0]
print "<hr>"
print "This is what I'm trying"
print """<img src="data:image/jpeg;base64,%s/>""" % data
######################################################################
if __name__ == "__main__":
form = cgi.FieldStorage()
if "show_file" in form:
print "Content-Type: text/html"
print
printHeaders("Image upload example")
showFile()
printFooter()