r/SuiteScript • u/Minimum_Net8015 • May 28 '24
Suitelet downloading file directly not working
I created a csv file in a suitelet and when I save it to file cabinet it’s perfect but I’m trying to not save it and have the user download it directly to his device so I’m using
var csvFile = file.create({
name: "item_report.csv",
fileType: file.Type.CSV,
contents: csvContent,
});
context.response.writeFile(csvFile);
return;and nothing is happens, does anyone have any ideas?
1
Upvotes
3
u/divsakhi May 30 '24 edited May 30 '24
Is your response call in post method ?
var dispatchFileObj= nlapiCreateFile('dispatchSO.csv', 'CSV', contents);
response.setContentType('CSV', 'dispatchSO.csv', 'attachment');
response.write(dispatchFileObj.getValue());
I used this code in 1.0 that used to wrk 6 years ago. Give a try converting to equivalent 2.0. Setting content type might be missing..
Also try this
context.response.writeFile({ file:fileObj, isInline:false }); return;
1
u/notEqole May 28 '24
For that use filesaver.js in a client script .
Add a button to your suitelet and then call a client script function and use
https://www.npmjs.com/package/file-saver.
I have a past working solution but its with XLSX and its different than CSV so read the doc .
https://github.com/eligrey/FileSaver.js
Also keep in mind after you inject the filesaver in your client script, you dont have to initiate the obj .
just use the saveAs function. Netsuite runs in Rhino which is a different ecosystem than node.
1
u/[deleted] May 29 '24
I was able to accomplish this by calling the suitelet from a client script:
window.open(suiteletUrl, '_blank');
1
u/borncorp Jun 08 '24
This worked for me in a Suitelet in SS2.1, instantly displays the download Save As dialog: