I was trying to load an image client side and base64 encode the bytes returned by the server in order to pass it off to perform some processing. IE has a RequestBody property of the XMLHttpRequest object, but I can't seem to use it, and RequestText is truncated. In Firefox, RequestText is there, but seems corrupted.
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to get the background from multiple images by
- void before promise syntax
You could have the server return base64 text, rather than doing that encoding client side.
For example, (in ASP.NET) a request to /ImageAsBase64.ashx?file=/images/myimage.png could be coded to read the file, base64encode it, and stream it as a response.
It's really going to be pretty much the same thing in PHP or whatever.
Here's how I did it.
This technique is provided in an answer to another SO question, but it's also relevant here.
I didn't want to base64 encode anything. I wanted to download and parse binary files in the browser via Javascript, without modifying the server to encode them specially. I found that in Firefox, by coercing the mimetype of the response via
overrideMimeType()
, I could useXMLHttpRequest.responseText
. On IE, it's different because:responseText
on IE truncates at the first zero. For binary streams this is a big problem.there is no
XMLHttpRequest.overrideMimeType()
, to force IE to treat binary streams as text.while there is a
XMLHttpRequest.responseBody
(IE only!) that is specifically designed to be used with binary data streams, maddeningly that property is not usable from Javascript.Therefore, the need is to convert IE's
responseBody
property into a thing that looks likeresponseText
from FireFox, with the mime-type coercion. This is possible using injected VBScript.To make it cross-browser, you need to just pack up the browser-specific logic in a conditional. This is what I used:
...then call
readByte(i)
to get the byte at the ith position in the binary file.Good luck.
Credit to Miskun for the VBScript conversion logic.
If you're using COTS, you could always set up an intermediate gateway wherein the request is made and transformed (base64 encoded in this case) into something more palatable before being returned to the client.