In some cases users would like to upload files to NetSuite File Cabinet in order to use them later.
However, it is not always easy to understand how it should be done. For instance, user may experience issues deciding at what point of the script execution it should create a file object and save it to the File Cabinet.
It is important that with SuiteScript 2.0, user needs to upload a file at a different stage of the script execution compared to SuiteScript 1.0.
To upload a file using an Assistant please follow the steps below.
- Create a Suitelet with SuiteScript 2.0
- Create an Assistant
- Create the steps of the Assistant
- Create a field with the File type on a step of your choice and initialize the other steps of the Assistant
- Save a file object to a variable during the POST request from  the context.request.files[‘file_id’] property.
 Note: It is impossible to pass this variable to the finish step of the Assistant directly to save the file there.
- Set the folder property of the file object to the folder of your choice
- Save the file using the save() method
Script :
/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 * @NModuleScope SameAccount
 */
define(['N/file', 'N/https', 'N/record', 'N/ui/serverWidget'],
/**
 * @param {file} file
 * @param {https} https
 * @param {record} record
 * @param {serverWidget} serverWidget
 */
function(file, https, record, serverWidget) {
    /**
     * Definition of the Suitelet script trigger point.
     *
     * @param {Object} context
     * @param {ServerRequest} context.request - Encapsulation of the incoming request
     * @param {ServerResponse} context.response - Encapsulation of the Suitelet response
     * @Since 2015.2
     */
    function onRequest(context) {
        log.debug('START');
        // Creates an Assistant and saved the assistant object to the variable
        var assist = serverWidget.createAssistant({
            title: 'Uploading File Assistant'
        });
        var step1 = assist.addStep({
            id: 'upload_file',
            label: 'Upload File'
        });
        var step2 = assist.addStep({
            id: 'upload_options',
            label: 'Options'
        });
        if (context.request.method == 'GET') {
        	// Runs if the assistant is not finished yet
            if (!assist.isFinished()) {
            	// Executes if the step of the Assistant is not being set yet
                if (assist.currentStep == null) {
                    assist.currentStep = assist.getStep({ id: 'upload_file' });
                }
                step = assist.currentStep.id; // gets the ID of the current Assistant step
                if (step == 'upload_file') {
                	// Creates a field of the FILE type used to upload a file
                    var uploadFile = assist.addField({
                    	id: 'custpage_file',
                    	type: serverWidget.FieldType.FILE,
                    	label: 'Upload a file'
                    });
			context.response.writePage(assist);
               } else if (step == 'upload_options') {
			context.response.writePage(assist); 
                }
            }
        }
        else if (context.request.method == 'POST') {
        	// Monitors the NEXT and BACK button clicks
            if ((assist.getLastAction() == serverWidget.AssistantSubmitAction.NEXT) || (assist.getLastAction() == serverWidget.AssistantSubmitAction.BACK)) {
             // uploading file to NetSuite may be done from the same step where the file is being added to a file type field
            	if(assist.currentStep.id == 'upload_file' ){
                    // creates a File object from the file type field
                    var file = context.request.files['custpage_file']; // gets the File object from the request
                    log.debug({
                    	title: 'file object',
                    	details: JSON.stringify(file)
                    })
                    // upload_option
                    file.folder = 223; // sets a Folder for the file
                    var fileId = file.save(); // saves the File to the Folder
                    log.debug({
                    	title: 'file id',
                    	details: JSON.stringify(fileId)
                    })
            	}
                assist.currentStep = assist.getNextStep();
                assist.sendRedirect({
                    response: context.response // redirects the user
                });
            } else if (assist.getLastAction() == serverWidget.AssistantSubmitAction.FINISH) {
                log.debug({
                	title: 'file id when finished',
                	details: JSON.stringify(fileId) // the fileId does not exist at this moment
                })
                assist.finishedHtml = 'FINISHED';
                context.response.writePage(assist);
                assist.isFinished = true;
           }
       }
   }
    return {
        onRequest: onRequest
    };
});