I want to send a BLOB to an APEX RESTful service. The payload has to be a JSON (not form-data, I've had a lor of problems with that in the server side).
The BLOB is an image that I need to upload to the DB, my problem is that I don't know how to send the data in the payload. Should it be a string that represents the array of bytes? Should it be the array itself? (I managed to extract a binary string with the readASBinaryString method of javascipt's FileReader).
Thanks for any help.
Your BLOB data could be encoded to Base64 and then transported in JSON document as a string. You can use window.btoa() to convert binary data to Base64 string.
For example:
{
"data": "SGVsbG8gd29ybGQ="
}
Related
I'm trying to implement NgOptimizedImage in my angular app.
<img [ngSrc]="imageUrl" width="196" height="196">
imageUrl is url to my server. The response is buffer. I used to convert the buffer to string in component.ts file and then using [src]="convertedBuffer" show the image. But NgOptimizedImage requires src, so it loads from the server when the image can be seen.
Do I have to convert the buffer to string on server, or is there a way to convert it to string on client side?
There is no way to do that. HTTP doesn't have native support for sending base64-encoded data, so the browser won't be able to decipher your image once it is requested.
Base64 is only supported in data URIs. It seems that you have to convert your server to send non-base64-encoded images or go back to your old way of displaying images.
The problem is on server side.
You have to send buffer and set "Content-Type": "image/TYPE" header.
If you're using express and nodejs here's how you can do it:
res.contentType('image/jpeg'); // set Content-Type header
res.send(buffer); // send buffer. make sure it is buffer (buffer instanceof Buffer)
I need to send audio data to an API which requires a base64 string as input.
Therefore I'm looking for a way to convert an Int16Array to a base64 string. Is there a simple procedure to do that? All methods that I've found so far applied to uint8 data...
I am using LZW algorithm for stringified JSON object to reduce size. Size reduction is fantastic, but I am having problems transmitting compressed string via AJAX because of URL malformed error. If I encode it with base64, the size goes up enormously.
What are the practices to send LZW compressed data to server ?
I am creating a web API with Node.js and Express and intend to use a single response to return an image and JSON data simultaneously. I saw a solution to specify headers: https://stackoverflow.com/a/18864738/1703565 but I do not want to perform two get requests. How do I accomplish this?
You could encode the image as a base64 string (http://nodejs.org/api/buffer.html#buffer_buf_tostring_encoding_start_end), and return this as part of your JSON.
Cookies
You could return the image as a normal image response body and then set a session cookie with the same request. The session cookie would contain the JSON data. Once the image loads you can pull the JSON data from the cookie via javascript.
This would work in all browsers. Only limitation would be the size of the JSON you could return. Max size looks to be 4093 bytes.
Image Encoding
If your JSON is too big to fit in a cookie then you could encode the image as a base64 object and return the image data in the JSON response.
In this case you would have to reconstruct the image on a canvas or use the image data url format to dynamically create an image object as the url in the comment from Kevin does.
One downside to image encoding would be the increased size of the response. Roughly, the size of the image after encoding in base64 would be 37% larger than the original image.
Ultimately it depends on your specific needs as to which method would best fit your requirements.
I am using Ajax to retrieve images from a remote server. First I try this directly using the URL of the remote server - the returned image is a string(since that's how Ajax communicates). I use the Javascript fromCharCode and CharCodeAt to convert the data back to binary and then the window.btoa() to display it. This works. Then I want to transfer this image through an overlay network (P2P). I intercept the Ajax request, transfer it to the server through the P2P network and then retrieve the response in []byte array. But now I need to know to what type of string I should convert the byte array before I feed it back to the calling Ajax client. If I use Base64 or simply convert the byte array to string it does not display the image correctly.
Anyone has tried working with something like this before?
I will appreciate any feedback very much. Thanks
Javascript doesn't have different kinds of strings.
The desired character set will be the same one the web page is encoded in, ideally UTF-8.
Have you compared the response sent by the P2P server to the response sent by the original server? Is there some kind of wrapper that's missing, or perhaps an important MIMEtype difference?