How to display database binary images on web pages

The first method: python processing, first save to the local, and form a url for access

try:
    file_name = "%s.JPEG" % re["zpid"]
    if re["zhaopian"]:
        with open(os.path.join(IMG_DIR, file_name), "wb") as fp:
            fp.write(re["zhaopian"].read())
            server.logger.info("Photo saved successfully!")
            re["zhaopian"] = "http://%s:%s/image/%s" % (SERVER_HOST,SERVER_PORT,file_name)
    if xczhaopian_re["zhaopian"]:
        with open(os.path.join(IMG_DIR,"xc_"+file_name), "wb") as xcfp:
            xcfp.write(xczhaopian_re["zhaopian"].read())
            server.logger.info("On-site photos saved successfully!")
            re["xczhaopian"] = "http://%s:%s/image/xc_%s" % (SERVER_HOST,SERVER_PORT,file_name)
except Exception as msg:
    re["zhaopian"] = ""
    re["xczhaopian"] = ""
    print("Error %s" % str(msg))

The second method: transcode b64 and then splicing codes for display: for example:

<img src=“data:image/png;base64,***************************************************"/>

For specific splicing, please correspond to the following codes according to the file type:

data:, text data
data:text/plain, text data
data:text/html,HTML code
data:text/html;base64,base64 encoded HTML code
data:text/css,CSS code
data:text/css;base64, base64 encoded CSS code
data:text/javascript,Javascript code
data:text/javascript;base64,base64 encoded Javascript code
data:image/gif;base64,base64 encoded gif image data
data:image/png;base64,base64 encoded PNG image
data: image/jpeg; base64, base64-encoded jpeg image
data: image/x-icon; base64, base64-encoded icon image data

Example of converting python to b64:

from base64 import b64encode
zp = b64encode(re["zhaopian"].read())
print(zp.decode("utf-8"))

Then splicing

Guess you like

Origin blog.csdn.net/lystest/article/details/130703130
Recommended