How to convert base64 text to a PNG image file in NetSuite

Recently, the client requested a feature to send PNG image file content to Netsuite as base64 text so that it would be converted to an actual file and stored in the file cabinet. Here is a sample function I created to test the functionality:

function createFileFromBase64(base64String){
 try {
      var folderId = 47609; // Folder ID where you want to store the file
      // Create the file
      var newFile = file.create({
             name: 'FILE' + new Date().getTime() + '.png',
             fileType: file.Type.PNGIMAGE,
             contents: base64String,
             encoding: file.Encoding.BASE64,
             folder: folderId
      });
      // Save the file to the file cabinet
      var fileId = newFile.save();
      log.debug('Image file saved with ID: ' + fileId);
 } catch (e) {
      log.debug('Error: ' + e.toString());
 }
}

Leave a comment

Your email address will not be published. Required fields are marked *