function writetofile($stringData, $myFile) { $fh = fopen('download/'. $myFile, 'w') or die("can't open file"); fwrite($fh, $stringData); fclose($fh); } $filename = $file. "_". date("d-m-Y_H-i",time()).
Let's start the steps. Steps 1: Installation - Firstly we will need to install the below npm command in your project. Steps 2: Import AngularCsv - Import AngularCsv in your component file where you want to add your download button to export your csv file. //Your data for download in csv file.
Expor websites to CSV There is no simple solution to export a website to a CSV file. The only way to achieve this is by using a web scraping setup and some automation. A web crawling setup will have to be programmed to visit the source websites, fetch the required data from the sites and save it to a dump file.
$http
service returns a promise
which has two callback methods as shown below.
$http({method: 'GET', url: '/someUrl'}).
success(function(data, status, headers, config) {
var anchor = angular.element('<a/>');
anchor.attr({
href: 'data:attachment/csv;charset=utf-8,' + encodeURI(data),
target: '_blank',
download: 'filename.csv'
})[0].click();
}).
error(function(data, status, headers, config) {
// handle error
});
Most of the references on the web about this issue point out to the fact that you cannot download files via ajax call 'out of the box'. I have seen (hackish) solutions that involve iframes
and also solutions like @dcodesmith's that work and are perfectly viable.
Here's another solution I found that works in Angular and is very straighforward.
In the view, wrap the csv
download button with <a>
tag the following way :
<a target="_self" ng-href="{{csv_link}}">
<button>download csv</button>
</a>
(Notice the target="_self
there, it's crucial to disable Angular's routing inside the ng-app more about it here)
Inside youre controller you can define csv_link
the following way :
$scope.csv_link = '/orders' + $window.location.search;
(the $window.location.search
is optional and onlt if you want to pass additionaly search query to your server)
Now everytime you click the button, it should start downloading.
var anchor = angular.element('<a/>');
anchor.css({display: 'none'}); // Make sure it's not visible
angular.element(document.body).append(anchor); // Attach to document
anchor.attr({
href: 'data:attachment/csv;charset=utf-8,' + encodeURI(data),
target: '_blank',
download: 'filename.csv'
})[0].click();
anchor.remove(); // Clean it up afterwards
This code works both Mozilla and chrome
This is what worked for me for IE 11+, Firefox and Chrome. In safari it downloads a file but as unknown and the filename is not set.
if (window.navigator.msSaveOrOpenBlob) {
var blob = new Blob([csvDataString]); //csv data string as an array.
// IE hack; see http://msdn.microsoft.com/en-us/library/ie/hh779016.aspx
window.navigator.msSaveBlob(blob, fileName);
} else {
var anchor = angular.element('<a/>');
anchor.css({display: 'none'}); // Make sure it's not visible
angular.element(document.body).append(anchor); // Attach to document for FireFox
anchor.attr({
href: 'data:attachment/csv;charset=utf-8,' + encodeURI(csvDataString),
target: '_blank',
download: fileName
})[0].click();
anchor.remove();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With