这是我的第一个问题。将存储在数据库中的BLOB转换为HTML网站上的图像
我让用户上传他们自己的图像到数据库。 该图像存储为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()
为什么不能将文件路径存储到数据库中的图像的任何原因? http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay?rq=1 – Jacob 2013-04-29 13:31:12
您需要包含更多来自您正在使用的脚本的代码。 – Aya 2013-04-29 14:10:50