问题
How can I upload my files to NetSuite file cabinet using SuiteScript 2.0?
回答1:
A possible option is to create a Suitelet with a Document field and save the file uploaded to that field. Here's the code for such a Suitelet:
/**
*@NApiVersion 2.x
*@NScriptType Suitelet
*/
define(['N/ui/serverWidget'],
function(serverWidget) {
function onRequest(context) {
if (context.request.method === 'GET') {
var form = serverWidget.createForm({
title: 'Simple Form'
});
var field = form.addField({
id: 'custpage_file',
type: 'file',
label: 'Document'
});
form.addSubmitButton({
label: 'Submit Button'
});
context.response.writePage(form);
} else {
var fileObj = context.request.files.custpage_file;
fileObj.folder = 4601; //replace with own folder ID
var id = fileObj.save();
}
}
return {
onRequest: onRequest
};
});
回答2:
I see you're talking about uploading files from local storage. You can create a file of specified type, based on the record you're working with in your script. By specifying a folder, this file can be saved, while also being available for further processing within your script.
I believe the below can be adapted to use your uploaded file by specifying the 'contents' as 'context.request.files.custpage_file' - as done in Maria's answer.
var exportFolder = '1254';
var recordAsJSON = JSON.stringify(scriptContext.newRecord);
var fileObj = file.create({
name: scriptContext.newRecord.id + '.json',
fileType: file.Type.JSON,
contents: recordAsJSON, description: 'My file', encoding: file.Encoding.UTF8,
folder: exportFolder,
isOnline: false
});
var fileId = fileObj.save();
fileObj.save() will return the internal ID of the newly created file in the file cabinet.
来源:https://stackoverflow.com/questions/49726167/how-to-upload-a-file-from-local-to-netsuite-file-cabinet-using-suitescript-2-0