RE: 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 🙂

Daryl Beginner Asked on March 8, 2021 in SuiteScript.
Add Comment
4 Answers

Has anyone made any progress on this.  I am trying to send PDFs to the SharePoint Microsoft Graph API.  I can always create the file, but it does not open correctly.  The only way I can get it to work is to send a local file from PostMan as binary.  When i make the same request to httpbin.org/put I can see the below.  Using getContents() from the NetSuite file returns a similar but different string.  Any Ideas?  Hoping there is some SuiteScript 2.1 of fetch breakthrough.

 

{
"args":{},
"data":"data:application/octet-stream;base64,JVBERi0xLjQKJeLjz9MKMTkgMCBvYmogPDwvU3VidHlwZS9Gb3JtL0ZpbHRlci9GbGF0ZURlY29kZS9UeXBlL1hPYmplY3QvTWF0cml4WzEgMCAwIDEgMCAwXS9Gb3JtVHlwZSAxL1Jlc291cmNlczw8L1Byb2NTZXRbL1BERi9UZXh0L0ltYWdlQi9JbWFnZUMvSW1hZ2VJXS9Gb250PDwvSGVsdiA3IDAgUj4+Pj4vQkJveFswIDAgMTY1LjM4IDIzLjk0XS9MZW5ndGggMTA0Pj5zdHJlYW0KeJwrVAhU0A+pUHDydVYoVDAAQkMzUz1jCwUjYz1LE4WiVI......
Rookie Answered on January 14, 2024.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.