I'm using HTML strings to output from the GET request of an external Suitelet to display a submission form. The form has a file input and 2 text inputs. The file metadata seems to be making it through to the POST as I have access to context.request.files.file_name.fileType and other fields via the full path (params.request.files.userfile) but there's no actual content here other than metadata. I say this because when I try to log params.request.files[0] or params.request.files.userfile I get empty string in logs.
The file get's created in Netsuite in the File cabinet as the correct file type and name, only the contents are empty.
Here's my form in the GET section of the Suitelet:
var html = '<form method="post" enctype="multipart/form-data" accept-charset="utf-8">';
html += '<input type="text" id="id" name="coolid" value="0"><br>';
html += '<input type="text" id="token" name="token" value=""><br>';
html += '<input type="file" id="file" name="file" accept="image/png">';
html += '<button type="submit">submit</button>';
html += '</form>';
params.response.addHeader({
name: 'Content-Type',
value: 'text/html; charset=UTF-8'
});
params.response.write({ output: html });
Here's the POST section of the Suitelet:
var fileObj = file.create({
name: params.request.parameters.coolid+ "_file",
fileType: params.request.files.userfile.fileType,
contents: params.request.files[0]
});
fileObj.folder = -1;
fileObj.save();
I can see in Firefox dev tools there is file data being sent from the Form :
And the file is being created in the Netsuite FileCabinet. Just with 0 bytes. Any clues to what I'm doing incorrect?
Suite answer ids 49537, 84073, and 82126 suggest that you don't need to use the file.create method and that the file is accessible by the ServerRequest.files property.
Replace:
var fileObj = file.create({
name: params.request.parameters.customer_ext_id + "_file",
fileType: params.request.files.userfile.fileType,
contents: params.request.files[0]
});
fileObj.folder = -4;
fileObj.save();
With:
var newFile = context.request.files['FILE_ID'];
newFile.folder = FOLDER_ID; //folder internal ID
var newFileId = newFile.save();
If you're not required/committed to using html the following should also work.
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);
}
if (context.request.method === 'POST') {
var newFile = context.request.files.custpage_file;
newFile.folder = 123;
var newFileId = newFile.save();
log.audit('file created', 'file id: '+ newFileId);
}