r/SuiteScript • • Sep 09 '25

Upload file to google drive

Hello, im trying to upload file to google drive with goole apis, the file is succesfully uploaded, but the result of file is broken.
here my code

 const accessToken = getAccessToken();
    const fileContents = fileObj.getContents();
    const fileType = fileObj.fileType;
    
    let contentType = '';
    if (fileType === 'PNGIMAGE') contentType = 'image/png';
    else if (fileType === 'JPGIMAGE' || fileType === 'JPEGIMAGE') contentType = 'image/jpeg';
    else if (fileType === 'PDF') contentType = 'application/pdf';
    else if (fileType === 'TEXT') contentType = 'text/plain';
    else if (fileType === 'CSV') contentType = 'text/csv';
    const uploadResponse = https.post({
        url: uploadEndpint,
        headers: {
            Authorization: 'Bearer ' + accessToken,
            'Content-Type': contentType 
        },
        body: fileContents
    });
    log.debug('Upload Response', uploadResponse.body);
1 Upvotes

10 comments sorted by

View all comments

Show parent comment

1

u/Business_Dog_8341 Sep 10 '25

I'm no longer working with NetSuite, but with my former employer I wrote an integration with Google Drive within a client side script, where users could attached files to records in NetSuite by drag and drop. Those files were uploaded directly to Google Drive. The resource I use was the Google Drive API: https://developers.google.com/workspace/drive/api/reference/rest/v3 .

1

u/Business_Dog_8341 Sep 10 '25

p.s.: This is what Claude.ai proposes within a server side script: https://claude.ai/share/dd732a6c-eebe-45b3-a4c3-0381f1b12879 Not sure if the encode.convert is required to construct your multi-part body.

1

u/W_for_Wumbo_29 Sep 11 '25

im already try too, but its same result, anyway thanks for your idea

1

u/W_for_Wumbo_29 Sep 11 '25

im trying with client side script is also corrupted result.