I am sending the document as byte array to an API that modifies the file and gets me a new file as a byte array.
Is it possible to replace document that I am working with currently, with the document received from the API?
If you can convert the byte array to a base64 string, then you can use the Body.insertFileFromBase64 method (with the Replace option) to do what you want. There are lots of StackOverflow answers about converting byte arrays to base64 string. For more about the Body.insertFileFromBase64 method, see Body.
Related
I have a rather long JSON file that I am using https.request to get from a URL. When I run JSON.parse on the string that I receive, I get an "Unexpected end of JSON input" error because it seems like the JSON.parse has a limit to how many characters it can parse and it will cut it off around halfway through my JSON file. Is it possible to somehow parse only half of the string, or retrieve only half of a JSON file from a URL? I am using Javascript.
What you're looking at is at SAX-like parser. First search: is there a SAX parser that reads json and fires events ... or streaming json parser (duplicate)
Basically, when one reads a large file, one has to know if can keep the whole file as a buffer in memory or one can read it in chunks (or per char) and you can interpret it in chunks (as stream).
In my Gnome Extension I would like to call GLib.convert. Sadly it does not work with strings but wants a ByteArray. Now I wonder how to convert a Javascript String into a UTF-16 Byte Array.
Bonus points if this uses some part of the Gnome bindings instead of implementing in Javascript.
ByteArray.fromString(someString, 'UTF-16') will convert your JS string into a UTF-16-encoded Uint8Array. This can be passed to GLib.convert (although, since fromString should understand all the encodings that GLib.convert does, maybe you don't need to after that?)
See also the ByteArray documentation.
What is the correct way to insert Buffer as BLOB just 1-to-1 without serializing to string/hex? Is it possible at all from Node.js?
At my backend I already have Buffers with binary data and want to store them with minimum overheads using prepared statements. I'm getting ER_TRUNCATED_WRONG_VALUE_FOR_FIELD errno:1366 Incorrect string value error. It looks like the library just assumes there is a valid utf8 string in a buffer.
I am saving a string to a data tag in html to be used by javascript and sent to a php API. I am working with a string that looks like:
\n\u003\u0010\u0001\u0018
After I save it to the data variable, it looks like:
↵0
which is fine. The original string still appears to be intact if you do a decodeURLComponent on it.
What I'd like to do is either decode the symbols to the original string in PHP or javascript because it needs to be consumed as the original string, but I'm struggling in both spots to get it done. Thoughts?
You can use Base64 to encode and decode string.
There are some example for javascript on the documentation.
And PHP's documentation for encode and decode..
I'm trying to decode a base64 string representing an image stored in a db.
I tried many libraries and solutions provided on SO, but I'm still unable to decode the image correctly. In particular, using the following code:
var img = new Buffer(b64, 'base64').toString('ascii');
I get a similar binary representation, except for the first bytes.
This is the initial part of the base64 string:
/9j/4RxVRXhpZgAASUkqAAgAAAANADIBAgAUAAAAqgAAACWIBAABAAAAiwYAABABAgAIAAAAvgAA
Here are the first 50 bytes of the original image:
ffd8ffe11c5545786966000049492a00080000000d003201020014000000aa00000025880400010000008b06000010010200
And here are the first 50 bytes of the string I get with javascript:
7f587f611c5545786966000049492a00080000000d0032010200140000002a00000025080400010000000b06000010010200
How you can see, the two strings are identical except for the fisrt 3 bytes and some few bytes in the middle.
Can somebody help me understand why this is happening and how to solve it? Thanks
The problem is that you're trying to convert binary data to ASCII, which most likely than not, will mean loss of data since ASCII only consists of values 0x00-0x7F. So when the conversion takes place, all bytes > 0x7F are capped at 0x7F.
If you do this instead, you can see the data matches your first 50 bytes of the original image:
console.log(Buffer.from(b64, 'base64').toString('hex'));
But if you want to keep the binary data intact, just keep it as a Buffer instance without calling .toString(), as many functions that work with binary data can deal with Buffers (e.g. fs core module).