這是我的第一個問題。將存儲在數據庫中的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