How to render base-64 string in <img> tag without knowing extension

。_饼干妹妹 提交于 2021-02-11 06:16:43

问题


We have a small feature on our site which allows users to upload a profile picture. This picture will then be stored in our Postgres database as a base-64 encoded string. My question concerns the data scheme used in an <img> URL to render a base-64 encoded string, e.g.

<img src="data:image/gif;base64,iVBORw0KGgo ...">
                     ^^^   but what if we don't know the extension?

We plan on simply storing a base-64 blob and nothing else. But in this case, how could we know what extension was used in the original file? I have briefly tested Chrome and IE 10, and they appear to be happy opening a PNG file with any image extension whatsoever, e.g. even data:image/zzz seemed to work. But I have read elsewhere that for certain image formats, e.g. TIFF, the correct extension must be given.

If having the correct extension be mandatory to render a base-64 string in an <img> tag, then does this mean that we would need to send the file extension to the UI in addition to the base-64 string itself?


回答1:


I will offer this as a partial answer to my own question. As it turns out, most base 64 libraries and tools actually generate the data:image/ext portion of the encoded string automatically, along with the actual base64 data. Hence, at least for our production setup, knowing the extension was a moot point because the Apache library we were using took care of this automatically.



来源:https://stackoverflow.com/questions/41136119/how-to-render-base-64-string-in-img-tag-without-knowing-extension

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!