小编use*_*223的帖子

将存储在数据库中的BLOB转换为HTML网站上的图像

这是我的第一个问题.

我让用户将自己的图像上传到数据库.该图像存储为BLOB.

我成功地做到了这一点.我正在使用MySQL作为数据库.

我遇到问题的部分是在调用时将BLOB显示为网站上的图像.

现在只显示二进制数据,显示许多奇怪的符号.我认为这是HTTP标题的问题.现在它在:

print "Content-Type: text/html"
Run Code Online (Sandbox Code Playgroud)

我试过了:

print "Content-Type: image/jpeg"
Run Code Online (Sandbox Code Playgroud)

我使用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()
Run Code Online (Sandbox Code Playgroud)

html python database blob image

5
推荐指数
2
解决办法
2万
查看次数

标签 统计

blob ×1

database ×1

html ×1

image ×1

python ×1