Download CSV jsReport File in client



  • Hello, I created a CSV jsReport but having difficulties loading the report in the screen or downloading it.
    My PDF Reports work great, and I was trying to use same approach as the PDF, but changing the Content Disposition, so basically, it is something like this:

    const reportResponse = await this.client.render({        
            template: {
              name: report.templateName,
            },
            data
          });
        
    const buffer = await reportResponse.body();
        
    response.set({
          'Content-Type': 'text/csv',
          'Content-Disposition': 'attachment; filename=example.csv',
          'Content-Length': buffer.length
        });
    
    response.end(generatedReport.buffer); 
    

    So as I mentioned before this code is working to render PDF, but it does not work for CSV. When I look into the Console errors, I'm getting error "Unexpected token R in JSON at position 0", where R is the first character of the first header column of the CSV file. I can see in the Network Response tab in chrome that I get a Response back with all the content of the file, but it seems that it's trying to parse the text of the csv file as a JSON object.
    Second attempt was trying to install jsReport browser-client (npm i @jsreport/browser-client), add "import jsreport from '@jsreport/browser-client', and use:

    const report = await jsreport.render({
          template: {
            name: reportName
          },
          data: reportData    
        })
        report.download('covid19.csv')'''
    

    But this approach is given error: SyntaxError: Unexpected token 'export' at ....api\webpack:\external "@jsreport\browser-client":

    Is there a CSV example jsReport Client implementation I could use?

    I will appreciate any help I can get on this topic. Thanks



  • I can see in the Network Response tab in chrome that I get a Response back with all the content of the file, but it seems that it's trying to parse the text of the csv file as a JSON object.

    This would mean that your client code is the problem because it is trying to parse the response.
    I don't know how is your client implemented so it's hard to say exactly what is the problem.

    But this approach is given error: SyntaxError: Unexpected token 'export' at ....api\webpack:\external "@jsreport\browser-client":

    Do you use some starter kit to set up webpack? I would try to import the jsreport client and find where is the problem.


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.