【发布时间】:2013-04-23 04:34:33
【问题描述】:
这是我的第一个问题。
我让用户将他们自己的图像上传到数据库。 该图像存储为 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()
【问题讨论】:
-
为什么不能只在数据库中存储图像的文件路径? stackoverflow.com/questions/3748/…
-
您需要包含更多来自您正在使用的脚本的代码。
标签: python html database image blob