Search code examples
javascriptcsvnetsuitehttpresponsesuitescript2.0

NetSuite - writing a file to Suitelet response to directly download file


Hope you can help. Something which appears so small is bugging the heck out of me!!

All I want to do in SuiteScript 2.0 is directly download a CSV file to the client as soon as I hit the SUBMIT button on a Suitelet. My code downloads to the client, but the content in the file is incorrect.

The following code creates the file (successfully):

    //creating CSV file
    var fileObj = FILEMODULE.create({
        name: 'ThreePLreport.csv',
        fileType: FILEMODULE.Type.CSV,
        contents: contents
    });  

The last snippet of code writes the file to the client side:

   context.response.writeFile(fileObj,true);

Now - when I come to open the file, it looks like this:

CSV file output from suitelet

When I save the same file on the filing cabinet, it looks correct:

enter image description here

I have tried adding HEADERS to the code, like:

context.response.addHeader({
        name: 'Content-Type:',
        value: 'text/csv' //text/csv
        });
context.response.addHeader({
        name: 'Content-Disposition',
        value: 'attachment; filename="report1.csv"'
        });

This doesn't seem to help in correcting the content inside the CSV file. Still displays the HTML code as you saw above.

Any ideas?

Thanks in advance!


Solution

  • I've done almost exactly this many times. It looks like you are not returning after you write the file. e.g.:

    context.response.writeFile({
        file:fileObj,
        isInline:false
    });
    return;