cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Tell us what you want to see on the Community here!
Close

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to encode/decode correctly using NetSuite file upload in Suitelet

Highlighted

How to encode/decode correctly using NetSuite file upload in Suitelet

Explorer | Level 3

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
        };
    });

 

 

 

4 Replies 4
Highlighted

Re: How to encode/decode correctly using NetSuite file upload in Suitelet

Dropboxer

In your code, I see you're uploading the data in the 'decodedStr', which is defined via 'var decodedStr = toBase64(fileContents);'. That seems like it may be the opposite of what you want to do though. The Dropbox API does not expect the uploaded data to have a layer of base64 encoding and will not automatically undo any such layer set by the client. Further, it looks like the original contents in 'fileContents' may already be base64-encoded, so "toBase64" may be the wrong thing to do anyway. Did you perhaps mean to implement and use a "fromBase64" instead? (That would also make more sense given the variable name 'decodedStr'.)

Highlighted

Re: How to encode/decode correctly using NetSuite file upload in Suitelet

Explorer | Level 3

Thanks for the response. I've actually tried both ways. Can you point me to the correct format needed for the body of the request? I've been attempting to find an example for some time but all I can find is data-binary: @filename.docx. I'm looking for how to add the body value without referencing the file from an <input> but through NetSuite's N/file module/object. 

 

 

Highlighted

Re: How to encode/decode correctly using NetSuite file upload in Suitelet

Explorer | Level 3

I was able to get a .rtf to upload with the correct contents (simple text phrase: "This is text") using the following:

// 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 = fromBase64(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
                });

function fromBase64(stringInput){
            return encode.convert({
                string: stringInput,
                inputEncoding: encode.Encoding.BASE_64,
                outputEncoding: encode.Encoding.UTF_8
            });
        }


However, .docx and .pdf are not uploading with any content. Are those supposed to be in UTF_8 as well? Are there differences between the types of files. I'm just looking to upload .ai and .pdf file formats.

Highlighted

Re: How to encode/decode correctly using NetSuite file upload in Suitelet

Dropboxer

The /2/files/upload endpoint is a "content-upload" style endpoint, meaning it expects the raw file data to be sent in the HTTPS request body, with the "Content-Type: application/octet-stream" request header to match.

 

I can't offer help with working in NetSuite though, as it's made by a third party, so I don't know how data retrieval/handling works there.

Work Smarter with Dropbox

The way we work is changing. Share and discover new ways to work smarter with Dropbox in our community.

Sound good? Let's get started.
Who's talking

Top contributors to this post

What do Dropbox user levels mean?
Need more support?