n/File contents (base64) to Binary in Server Script
I’m trying to decode file.getContents() into Binary, in order to upload the file to another service, but it ends up corrupted.
The Base64 string from N/file is ASCII encoded, I’ve managed to get that far. But since I don’t have access to the normal JavaScript Blob/FileReader classes, I can’t use those.
I’ve tried using convert() from N/encode and getReader() + readChars() from N/file and neither work.
The closest I got was using the function from this gist, but it still ended up corrupted (just not as bad as other attempts).
I know it’s not an issue on the server side (where I’m sending it too), as I’ve sent stuff to it from normal JavaScript fine (using Blobs).
My header for the https.post() request set ‘Content-Type’ to ‘application/octet-stream’, as that is what the server is expecting.
So I’m confused. Is Netsuite doing something on https.post()? Is there a better way to decode the Base64?
Any help would be appreciated 🙂
Depends on what your file is and how the service you are working with handles strings. Either way, expect to suffer more as you discover how badly N/https handles binary data.
Fair chance that N/encode is inappropiate for your task, it only converts base64 to utf-16 strings. That will fail for binary data since it encodes the encoding errors in a way that is not reversible.
It honestly depends more on how your receiving service interprets the strings you generate. I’ve had minor success using js-base64. whichs may offer you hope since it has many options. Including an atob implementation. You might be able to use SuiteScript 2.1 to actually get access to typed arrays, but you will still have the primary problem of converting this to a string to use with N/https.
Be prepared to do this outside of NetSuite
Thanks for the quick response 🙂
Yeah I’ve already put in around a dozen hours playing around. My attempts with N/encode were mostly a last ditched effort to see if it worked. I’ll try js-base64 when I get a chance.
Does N/https screw around with the data you send it?
I dont think N/https does any manipulation outside of General Blocked HTTPS Headers
Its just thats it uses a string parameter for the post body, which is a terrible idea for binary data