Download text/csv content as files from server in Angular

前端 未结 5 1385
失恋的感觉
失恋的感觉 2020-11-28 02:29

I am trying to stream a csv file from a node.js server. The server portion is very simple :

server.get(\'/orders\' function(req, res) {
  res.se         


        
5条回答
  •  难免孤独
    2020-11-28 03:03

    Using angular 1.5.9

    I made it working like this by setting the window.location to the csv file download url. Tested and its working with the latest version of Chrome and IE11.

    Angular

       $scope.downloadStats = function downloadStats{
            var csvFileRoute = '/stats/download';
            $window.location = url;
        }
    

    html

     CSV
    

    In php set the below headers for the response:

    $headers = [
        'content-type'              => 'text/csv',
        'Content-Disposition'       => 'attachment; filename="export.csv"',
        'Cache-control'             => 'private, must-revalidate, post-check=0, pre-check=0',
        'Content-transfer-encoding' => 'binary',
        'Expires' => '0',
        'Pragma' => 'public',
    ];
    

提交回复
热议问题