0
簡單地說,我希望獲取在數據庫中的所有156個存儲的圖像和產生的HTML鏈接,如「<img src ...../> "
MySQL的編碼轉換成HTML
我的代碼工作在提取數據庫中的圖像和觀看他們極大的。但是當我開始測試base64編碼,我收到以下錯誤
return binascii.a2b_base64(s)
binascii.Error: Incorrect padding
這裏是代碼:
import MySQLdb
import base64
db = MySQLdb.connect(host="localhost", # your host, usually localhost
user="test", # your username
passwd="test", # your password
db="test")
cur = db.cursor()
cur.execute("SELECT * FROM attachments")
#looping through row 4 where all blobs are located
for row in cur.fetchall():
blob_read = row[4]
#(error happen here while decoding)
decoding = blob_read.decode('base64')
print '<img src="data:image/jpg;base64,"'+ decoding + '/>'
filename = row[2]
#(extracted the files to test if images are corrupted; they are not)
with open(filename, 'wb') as output_file:
output_file.write(blob_read)
db.close()