I'm trying to use NetSuite's N/file module to upload a file to DropBox. I've been successful at authentication, getting a 200 when submitting the POST request to Create FolderV2 and Upload File. However, I can't figure out how to get the file contents to set correctly. Currently, the file is being created with the some sort of encoding/decoding like this "UEsDBBQABgAIAAAAIQDfpNJsWgEAACAFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
Here's the code:
/**
*@NApiVersion 2.x
*@NScriptType Suitelet
*@NModuleScope Public
*/
define(['N/ui/serverWidget', 'N/file', 'N/https', 'N/encode'],
function(serverWidget, file, https, encode) {
function onRequest(context) {
if (context.request.method === 'GET') {
var form = serverWidget.createForm({
title: 'Upload Artwork',
hideNavBar: false
});
// Add File Upload
var fileFld = form.addField({
id: 'custpage_file',
type: serverWidget.FieldType.FILE,
label: 'Upload File'
});
form.addSubmitButton({
label: 'Upload'
});
context.response.writePage(form);
} else {
// Getting File from NS File Field
var fileObj = context.request.files['custpage_file'];
// Setting Folder in File Cabinet
fileObj.folder = 214960;
var fileId = fileObj.save();
// Getting/Logging File Contents
var fileContents = fileId.getContents();
log.debug({title: 'File Contents', details: fileContents});
// File Contents = UEsDBBQABgAIAAAAIQDfpNJsWgEAACAFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAACAAAAAAAAAAAAAAAAA
// Decode the String
var decodedStr = toBase64(fileContents);
log.debug({title: 'Decoded Contents', details: decodedStr});
var headerObj = {};
// Upload Request
var res = https.request({
method: https.Method.POST,
url: 'https://content.dropboxapi.com/2/files/upload',
body: decodedStr,
headers: headerObj
});
// Write Response
context.response.write(JSON.stringify(res));
}
}
function toBase64(stringInput){
return encode.convert({
string: stringInput,
inputEncoding: encode.Encoding.UTF_8,
outputEncoding: encode.Encoding.BASE_64
});
}
return {
onRequest: onRequest
};
});