Arif WB
Arif WB

Reputation: 316

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!

Upvotes: 3

Views: 6170

Answers (3)

Mirna
Mirna

Reputation: 11

I had the same problem, I thought it was the header but no. I realized that I was submitting the wrong content. The content must be an array separating each column by commas. The end of the line is '\ n'.

ex. ['title1','title2','title3','\n','contentline1','contentline1','contentline1','\n','contentline2','contentline2','contentline2','\n']

1.- Build the array 2.- Create the csv file

var downFile = file.create({
            name: somename + '_' + qty + '.csv',
            fileType: file.Type.CSV,
            contents: csvBody,
 });

3.- Download the file

context.response.writeFile({ file: downFile });

Upvotes: 1

bknights
bknights

Reputation: 15367

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; 

Upvotes: 7

Adolfo Garza
Adolfo Garza

Reputation: 3029

Try removing the inline option (display in browser) so change:

context.response.writeFile(fileObj,true);

to

context.response.writeFile(fileObj);

Upvotes: 0

Related Questions