Sunday, January 27, 2019

Decoding Base64 String in Java and C#

When a user requests a 'get' on a file in the file cabinet, NetSuite returns the content in Base64 format.

I. Decoding using Java

Note: Users need the following library - org.apache.commons.codec.binary.Base64

public class base64 {
    public static void main(String[] args) {
        // string to decode
        String decodeme = "ZnVuY3Rpb24gYWFyb24oKQ0bGRWYWx1ZSgnZXhwZWywnSGknKTsNCn0=";

        byte[] decoded = Base64.decodeBase64(decodeme.getBytes());
        String decodedString = new String(decoded);

II. Decoding using C#

RecordRef recordRef = new RecordRef();

// internal id of the file from the file cabinet
recordRef.internalId = "123"
recordRef.type = RecordType.file;
recordRef.typeSpecified = true;

ReadResponse ReadResponse = _service.get(recordRef);

// response received from Netsuite
File mediaFile = (File)ReadResponse.record;

//get content of the response
String base64BinaryStr = System.Convert.ToBase64String(mediaFile.content);
byte[] encodedDataAsBytes = System.Convert.FromBase64String(base64BinaryStr);

System.IO.File.WriteAllBytes(@"C:\Temp\myFile.jpg", encodedDataAsBytes);

No comments:

Post a Comment