Skip to content Skip to sidebar Skip to footer

Angular Requesting A Csv File Download Url, And Force Download

How to trigger a csv file download from within Angular on a button click? The backend is the php route. I tried solutions which adds an anchor tag dynamically and appends to it's h

Solution 1:

After fiddling with the existing answers, i tried the below and its worked.

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 = functiondownloadStats{
        var csvFileRoute = '/stats/download';
        $window.location = url;
    }

html

<atarget="_self"ng-click="downloadStats()"><iclass="fa fa-download"></i> CSV</a>

In php set the below headers for the response:

$headers = [
    'content-type'              => 'text/cvs',
    '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',
];

Post a Comment for "Angular Requesting A Csv File Download Url, And Force Download"