In NetSuite, saving files directly from a Client Script (CS) is not possible because the N/file module is only available on the server side. To work around this limitation, you can send file data from the browser to a Suitelet, which then saves the file to the File Cabinet using server-side APIs.
This article demonstrates how to implement this pattern, including example code, best practices, and a brief introduction to JavaScript file objects used in the browser.
NetSuite’s N/file module is restricted to server-side scripts (Suitelet, Scheduled Script, etc.) for security and platform reasons. Client Scripts run in the user’s browser and do not have access to server-side modules or the File Cabinet. Therefore, you must send the file data to a Suitelet, which acts as a secure server-side endpoint to handle the file save.
The recommended approach is: 1. Use browser JavaScript to read or generate the file (e.g., PDF, CSV). 2. Convert the file to a base64 string. 3. Send the file data to a Suitelet via an HTTP POST request. 4. The Suitelet saves the file using the N/file module.
This Suitelet receives a POST request with file data and saves it to the File Cabinet.
/**
* @NApiVersion 2.1
* @NScriptType Suitelet
*/
define(['N/file', 'N/log'],
function (file, log) {
function onRequest(context) {
if (context.request.method === 'POST') {
try {
const data = JSON.parse(context.request.body);
const nsFile = file.create({
name: data.filename,
fileType: file.Type.PDF,// Adjust file type as needed
contents: data.base64Data,
encoding: file.Encoding.BASE_64,
folder: 12345 // Replace with your folder ID or use a variable
});
const fileId = nsFile.save();
context.response.write(JSON.stringify({ success: true, fileId }));
} catch (e) {
log.error('File Upload Error', e);
context.response.write(JSON.stringify({ success: false, error: e.message }));
}
} else {
context.response.write('Only POST supported');
}
}
return {
onRequest: onRequest
};
});
Below is a sample Client Script function to send a file to the Suitelet:
// Example Client Script function
function saveFileToNetSuite(base64Data, filename, suiteletUrl) {
fetch(suiteletUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ filename: filename, base64Data: base64Data })
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert('File saved! File ID: ' + data.fileId);
} else {
alert('Error: ' + data.error);
}
});
}
When working with files in the browser, you will often use the following objects:
function fileToBase64(file, callback) {
const reader = new FileReader();
reader.onload = function(e) {
// e.target.result is a base64 string
const base64 = e.target.result.split(',')[1]; // Remove data:...;base64, prefix
callback(base64);
};
reader.readAsDataURL(file);
}
Client Scripts in NetSuite cannot save files directly to the File Cabinet because the N/file module is server-only. The work around is to send file data as base64/blob to a Suitelet, which saves the file securely. Understanding browser file objects like File, Blob, and FileReader is essential for preparing file data on the client side.
This approach is essential for scenarios like saving generated PDFs, uploading user files, or automating document storage from the browser.
About Us
Concentrus is a leading provider of innovative cloud-based enterprise resource planning (ERP) solutions, including NetSuite. Our team of NetSuite experts offers a range of services, including NetSuite price analysis, NetSuite training, and NetSuite integration services.
Our goal is to help businesses of all sizes maximize their investment in NetSuite by providing expert NetSuite cost optimization and implementation strategies. With years of experience as a NetSuite partner, our NetSuite administrators and NetSuite consultants are well equipped to help businesses of all sizes with their NetSuite consulting needs.
Whether you're looking for a NetSuite consultant to help with your NetSuite implementation or you need ongoing NetSuite support, Concentrus is here to help.
Read About Our Implementation Methodology
Want more NetSuite Tips and Tricks? Check out our Short & 'Suite videos