Angular 2 download .CSV file click event with authentication

后端 未结 2 876
灰色年华
灰色年华 2021-01-02 03:41

I\'m using a spring boot backend and my api uses a service to send data via an OutputStreamWriter. I can download this in Angular 2 using a click event like so:

相关标签:
2条回答
  • 2021-01-02 03:49

    Looks like you just need to parse the body of the response i.e

    let parsedResponse = data.text();
    this.downloadFile(parsedResponse);
    

    Also I would recommend you use FileSaver to download files as even in 2016 there does not seem to be a standard way to do this across browsers.

    let blob = new Blob([data], { type: 'text/csv' });
    saveAs(blob, "data.txt");
    

    For a more in depth guide check here

    0 讨论(0)
  • 2021-01-02 03:57

    I use FileSaver, too. If you have extension on client side, you can see that it will work properly for CSV files. You just need to add extension manually:

    FileSaver.saveAs(res, 'export' + extension);
    
    0 讨论(0)
提交回复
热议问题