【问题标题】:Base64 image not getting shown in internet explorerBase64 图像未在 Internet Explorer 中显示
【发布时间】:2020-03-30 12:03:52
【问题描述】:

我有一个代码可以获取 base 64 格式的图像,如下所示:-

import base64
import requests
img=requests.get('https://example.com/hello.jpg')
base=base64.b64encode(img.content)
base=base.decode('utf-8')
print(base)

html:-

<img src="data:image/jpeg;base64,<base64_data>" width="20" height="20">

我可以获得 base64 格式的图像,并可以在浏览器中使用它们,例如 html 格式的 chrome。但在 Internet Explorer 中,我只能显示一些图像。显示带有短 base64 的图像,但长则不显示。我该如何解决?

【问题讨论】:

    标签: python html image internet-explorer base64


    【解决方案1】:

    但在 Internet Explorer 中,我只能显示一些图像。图像与 显示了短 base64,但长时没有。我该如何解决?

    您的意思是图像仅显示部分,例如this?我想这个问题可能与您获取 Image base64 字符串的方式有关。您是从打印结果中获取 base64 字符串,对吧?

    我已经使用您的代码创建了一个示例,在设置断点以检查 base 值并打印结果之后,似乎 print() 方法只打印了 base64 字符串的一部分。截图如下:

    所以,我建议您可以尝试将 base64 字符串写入 .txt 文件,并从中获取 base64 字符串。代码如下:

    import requests, base64, sys
    from io import BytesIO
    img=requests.get('https://i.stack.imgur.com/eldOQ.jpg')
    base=base64.b64encode(img.content)
    base=base.decode('utf-8')
    print("****************")
    print(base)
    print("****************")
    text_file = open("output.txt", "w")
    n = text_file.write(base)
    text_file.close()
    

    使用整个base64字符串后,结果类似于this

    【讨论】:

    • 我的意思是在 Internet Explorer 中显示的 base64 字符串长度较短的图像。但不会解释长 base64 字符串。其他浏览器可以完美做到。
    • 你按照上面的回复查过值了吗?你的意思是使用整个base64字符串(长base64字符串),它仍然无法在IE浏览器中工作?如果是这样,您可以分享相关的base64字符串或分享图像吗? (因为,正如您在我之前的回复中看到的,代码在我这边运行良好)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    • 2013-03-20
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    相关资源
    最近更新 更多