在SRC标签显示图像响应文本没有的base64有的、图像、文本、标签

2023-09-10 15:00:28 作者:误入情途

我有一个图像返回为GIF POST请求。至于我可以告诉大家的反应是不是BASE64 EN codeD文本。所以,我怎么能图像分配给IMG的src标签?

I have a POST request that is returning an image as a gif. As far as I can tell the response is not base64 encoded text. So how can I assign the image to the src tag of an img ?

我说的不是Base64的EN codeD,因为我已经尝试过使用数据来显示它的原因:图像/ GIF; BASE64,+数据,这给予我没有结果。

The reason I say its not base64 encoded as I have tried to display it using data:image/gif;base64,"+ data and this give me no result.

于是我试着用jquery.base64插件编码repsonse(但抱怨无效字符);

So then I tried encoding the repsonse using the jquery.base64 plugin (but that complained about invalid characters);

响应文字看起来就像小提琴手下面的GIF87a,看着响应的TextView。图像视图显示图像细腻。

the response text looks like the following "GIF87a��" in fiddler, looking at the response textview. image view displays the image fine.

我没有访问服务器,告诉它以base64回任。

I have no access to the server to tell it to return in base64 either.

任何意见或帮助将是很大的AP preciated,也许它这么简单的东西我已经错过了。

Any ideas or help would be greatly appreciated, maybe its something so simple I've missed it.

在此先感谢。

推荐答案

这是完全可能的:

下面是例子:

var req = new XMLHttpRequest;
req.overrideMimeType('text/plain; charset=x-user-defined');
req.open('GET', "https://m.xsw88.com/allimgs/daicuo/20230910/420.png.jpg", !1);
req.send(null);
for (var responseText = req.responseText, responseTextLen = responseText.length, binary = "", i = 0; i < responseTextLen; ++i) {
  binary += String.fromCharCode(responseText.charCodeAt(i) & 255)
}
image.src = 'data:image/jpeg;base64,'+window.btoa(binary);

这是例子来自互联网,但证实了它的工作原理。 POST建议立即进行删除的工作方式完全相同。

This is example from Internet, but confirmed that it works. 'POST' shoul work exactly the same way.