RE: Has anyone in the NS developer world ever push/upload a csv file from external drive into Netsuite file cabinet using a RESTlet
I implemented (tried to) the file-cabinet-api restlet (Tim D) and testing with Postman. My Tokens are good as I tested with a GET call using a simple Hello World function. When I try to use the fileCreate function in the restlet to upload a file from my local drive to the file cabinet, it fails. I used the Hello World example in the script, but no file was created. I tried fileGet and it didn’t work either.
I have searched and searched and it seems that no one has done this using a NS RESTlet. I have successfully implemented an email capture plug-in that pushed a csv file into the File Cabinet, but when you have financial data, email is unacceptable.
Thanks for any help. Since I’m new, I will continue to search this community.
Use the below code, besides images it works well for csv, txt, log and PDF
/**
* @NApiVersion 2.x
* @NScriptType Restlet
*/
define([‘N/file’, ‘N/encode’, ‘N/log’], function (file, encode, log) {
functionpost(context) {
log.debug(‘Received context’, JSON.stringify(context));
try {
varbase64Content=context.fileContent;
varfolderId=context.folderId;
varfileName=context.fileName;
varextension=fileName.split(‘.’).pop().toUpperCase(); // Get the file extension
// Decode the base64 content for text-based files
if ([‘TXT’, ‘CSV’, ‘LOG’, ‘TEXT’].includes(extension)) {
base64Content=encode.convert({
string:base64Content,
inputEncoding:encode.Encoding.BASE_64,
outputEncoding:encode.Encoding.UTF_8
});
}
varfileTypeMapping= {
‘PDF’:file.Type.PDF,
‘JPG’:file.Type.JPG,
‘JPEG’:file.Type.JPG,
‘CSV’:file.Type.CSV,
‘TXT’:file.Type.PLAINTEXT,
‘TEXT’:file.Type.PLAINTEXT,
‘LOG’:file.Type.PLAINTEXT
};
varfileType=fileTypeMapping[extension] ||file.Type.PLAINTEXT; // Default to plain text if extension is not recognized
log.debug(‘Creating file’, { name:fileName, type:fileType });
varuploadFile=file.create({
name:fileName,
fileType:fileType,
contents:base64Content,
folder:folderId
});
log.debug(‘Saving file’);
varfileId=uploadFile.save();
varfileRecord=file.load({
id:fileId
});
varfileUrl=”https://3659948-sb1.app.netsuite.com”+fileRecord.url;
log.audit(‘File uploaded successfully’, fileUrl);
return {
success:true,
message:’File uploaded successfully!’,
fileId:fileId,
fileUrl:fileUrl
};
} catch (e) {
log.error(‘Error in file upload’, e.toString());
return {
success:false,
message:e.toString()
};
}
}
return {
post:post
};
});
Encode base64 at source
with open(file_name, “rb”) as f:
file_contents=f.read()
base64_bytes=base64.b64encode(file_contents) # Here you should use file_contents, not f.read()
payload= {
‘fileName’: file_name,
‘fileContent’: base64.b64encode(file_contents).decode(‘utf-8’), # Assuming file_contents is your binary file content
‘folderId’: folder_id
}
file_name = “potantial_delivered.csv”
with open(file_name, “rb”) as f:
file_contents=f.read()
base64_bytes=base64.b64encode(file_contents) # Here you should use file_contents, not f.read()
folder_id=”8977501″
upload_to_ns(file_name, base64_bytes, folder_id)
2ps
If you can post sample code for your restlet, we can walk you through the suitescript to save the file to the filecabinet. You can also do this easily using suitetalk.
soakes55
var file,log;
define( [ ‘N/file’, ‘N/log’], main );
function main( fileMod, logMod ) {
file = fileMod;
log = logMod;
return {
post: postProcess
}
}
function postProcess( request ) {
if ( ( typeof request.function == ‘undefined’ ) || ( request.function == ” ) ) {
return { ‘error’: ‘No function was specified.’ }
}
switch ( request.function ) {
case ‘fileCreate’:
return fileCreate( request )
break;
default:
var response = { ‘error’: ‘Unsupported Function’ }
return response;
}
}
function fileCreate( request ) {
// Load the file.
try {
var myfile = file.create(
{
name: request.name,
fileType: request.fileType,
contents: request.contents,
description: request.description,
encoding: request.encoding,
folder: request.folderID
}
);
// Save the file and get its ID.
var fileID = myfile.save();
// Load the file.
myfile = file.load( { id: fileID } );
// Create the response.
var response = {};
response[‘info’] = myfile;
response[‘content’] = myfile.getContents();
return response;
} catch (e) {
return { ‘error’: e }
}
}