将存储在数据库中的BLOB转换为HTML网站上的图像 [英] Converting BLOB, stored on a database, to an image on an HTML website

查看:539
本文介绍了将存储在数据库中的BLOB转换为HTML网站上的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个问题.

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

I am having users upload their own image to a database. That image is stored as a BLOB.

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

I was able to do this successfully. I am using MySQL for the database.

我遇到麻烦的部分是当其被调用时将BLOB作为图像显示在网站上.

The part I am having trouble with is displaying that BLOB as an image on the website when its called upon.

现在仅二进制数据,显示许多奇怪的符号.我认为这与HTTP标头有关.现在它在:

Right now only the Binary data, lots of weird symbols are being displayed. I think its a problem with the HTTP header. Right now its in :

print "Content-Type: text/html"

我尝试过:

print "Content-Type: image/jpeg"

我正在使用Python连接数据库并编写HTML.

I am using Python to connect with the database and write the 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()

推荐答案

图像以二进制格式存储在数据库中,因此一旦使用解码功能将其传回服务器,图像就返回服务器

image is stored in database in binary format so once it comes to server using decode function to get it back to image

image.decode('base64')

这会将您的Blob转换为图片

this will convert your blob to image

这篇关于将存储在数据库中的BLOB转换为HTML网站上的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆