Convert buffer to base64 string for NgOptimizedImage - javascript

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)

Related

Compress Data in JavaScript and send it to Flask Server

So my team has made a small tool to perform Image Annotation and Labeling. And we were trying to optimize our code.
We were able to compress data from the server, but we have been trying to perform compression on data that is being sent from client to server.
What data you may ask, its just text file around 2 - 3mb.
Is there any way we can perform compression?
We are using JavaScirpt and want to send to FLASK.
This is the first question i am posting on here :)
You can try with this lib: Paco-zlib
var binaryString = pako.deflate("2-3mb text content", { to: 'string' });
// Here you can do base64 encode, make xhr requests and so on.
But at server side, responding page will receive compressed data. You shoud decompress it using something like zlib.decompress(compressedData)

Sending BLOB to APEX RESTful service

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="
}

Send image and JSON data in single Node.js + Express response

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.

Best format to send image from javascript client to SQL server

I am making an application that will store a Azure SQL server DB user information, including profile photo downloaded from Facebook. On the server side, ASP.NET MVC4'll have a controller that will receive the information and send it to the database.
The client side is Javascript and thought to give the image in json (once converted to base64). Is it a good option? Is it better to directly send the jpg? What are the advantages of sending information in json?
In SQL Server image field would be stored as a nvarchar (max)
Are you going to return the image as a binary stream content type image/jpeg or as a text stream encoded base64? Is far more likely that you're going to do the former, so there is little reason to go through an intermediate base64 encoded transfer. And of course, store them as VARBINARY(MAX). Even if you would choose to store them as base64, choosing an Unicode data type for base64 text is really wasteful, (double the storage cost for no reason...), base64 can fit very well in VARCHAR(max).
But, specially in a SQL Azure environemnt, you should consider storing media in Azure BLOB storage and store only the Blob path in your database.
In my opinion, it's better sending the image directly in .jpg using Multipart Forms or something like that.
Sending information in Json is useful when you transfer explicit data, like collections or objects that you will be able to query or de-serialize later.
The client side is Javascript and thought to give the image in json (once converted to base64). Is it a good option?
As Pasrus pointed out, you are not going to manipulate the image data. So JSON does not seems to be a good choice here.
One option is, you can add the base64 data into src attribute in html tag and send it.
What are the advantages of sending information in json?
Please check this answers and there are so many:
Advantages of using application/json over text/plain?
In SQL Server image field would be stored as a nvarchar (max)
Please refer this link:
Storing images in SQL Server?

P2P Ajax image transfer

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?

Categories

Resources