I'm working on a Suitelet in Vue3 and Axios to sav...
# suitescript
s
I'm working on a Suitelet in Vue3 and Axios to save file data to the NetSuite file cabinet. Text files are working, but I'm having trouble with images and PDFs. Do I need to convert the file to base64? Any tips for handling different file types in SuiteScript? Any advice would be super helpful. Thanks a lot! My current script:
Copy code
const fileData = uploadFile.buffer.data;
const fileType = mimeTypeToFileType[uploadFile.mimetype];
log.debug(LOGTITLE, `fileType: ${uploadFile.mimetype}, ${fileType}`);

let binary = '';
const bytes = new Uint8Array(fileData);
bytes.forEach(byte => binary += String.fromCharCode(byte)); // For txt files, the binary value is identical to the original file content.
            
let base64 = '';
for (let i = 0; i < binary.length; i += 3) {
    const byte1 = binary.charCodeAt(i);
    const byte2 = binary.charCodeAt(i + 1);
    const byte3 = binary.charCodeAt(i + 2);

    const chr1 = byte1 >> 2;
    const chr2 = ((byte1 & 3) << 4) | (byte2 >> 4);
    let chr3 = ((byte2 & 15) << 2) | (byte3 >> 6);
    let chr4 = byte3 & 63;

    if (isNaN(byte2)) {
          chr3 = chr4 = 64;
    } else if (isNaN(byte3)) {
          chr4 = 64;
    }

    base64 += String.fromCharCode(chr1 + 65, chr2 + 65, chr3 + 65, chr4 + 65);
}

const fileObj = file.create({ 
    name: uploadFile.originalname,
    fileType: fileType,
    contents: base64,                          // Need to convert this value to binary to make your txt file work
    folder: requestBody.folderId,
});
const fileId = fileObj.save();
b
typically btoa and atob are the usual first responses
there are more advanced things you can do, but you can use stackoverflow for those
s
Do you think it's possible to use btoa in suitescript?
b
in client script, sure, serverside no
s
Maybe, and this is Suitelet
b
its extra weird to work with binary data in suitescript without it being in base64 in the first place
s
I'm not trying to upload file using NS standard file field
b
mostly because there is the i/o options provided by suitescript dont provide binary data
again, its more likely that you arent being specific enough in your description
the suitelet is the code that runs serverside in response to a http request
s
This file info is sent by API request from 3rd party
b
its response is rendered in the user's browser, which commonly is html and can have its own code running in it
this would be client side and generally supports more normal javsscript, and would be where you would use vue
it is exteremely unlikely that you will receive binary data that isnt base64 in the actual suitelet, suitescript doesnt acually provide any apis to do so
s
Let's say there is a website developed in Vue. And after uploading files in front-end, I would like to store uploaded files in NetSuite FileCabinet
b
by vue or in vue
extra weird to be by vue
s
I sent these files using axios
b
so far so good, but again, that would be in the response rendered by the browser,
not the actual suitelet itself
s
Okay, thanks for your help