-
I have a string with the contents of a binary file, but when I write this string to a file using |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
I think this is because writeFile is doing to first UTF8 encode the string and then write the result, which is not what you want for a binary file. Assuming |
Beta Was this translation helpful? Give feedback.
-
This seems to be one of many oversights from Google and the documentation they provide. The I managed to brute-force it by looping over the string, getting each char and then passing that to a Uint8Array. const res = await gapi.client.drive.files.get({
'fileId': file.id,
'alt': 'media'
});
var bytes = [];
for (var i = 0; i < res.body.length; ++i) {
bytes.push(res.body.charCodeAt(i));
}
FS.writeFile("/GoogleDrive/" + file.name, new Uint8Array(bytes)); Which works, but it feels like the wrong solution. I'm open to suggestions on a better way. |
Beta Was this translation helpful? Give feedback.
This seems to be one of many oversights from Google and the documentation they provide. The
"body"
is a File blob according to the docs. It does not specify how to choose whether it's represented as a string or an ArrayBuffer, just that you can. Apparently, this is set in theGoogle.drive()
constructor which doesn't exist in the JS version of the api. The discussions I could find for fixing this ended with one dev team blaming another.Sorry for the mini rant, on to my "solution".
I managed to brute-force it by looping over the string, getting each char and then passing that to a Uint8Array.