Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

ajax - Display image in src tag with response text not base64

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 ?

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.

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

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

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

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

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is totally possible:

here is example:

var req = new XMLHttpRequest;
req.overrideMimeType('text/plain; charset=x-user-defined');
req.open('GET', "http://jonathanleighton.com/images/me.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);

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...