I need to create a csv file based on passed parameters.
I want to call my function from angular service using controller's remoteAction.
How can I make the browser save the string output as a file?
1 Answer
You can use the following code:
var link = document.createElement('a');
link.download = 'data.json';
var blob = new Blob([your_string], {type: 'text/csv'});
link.href = window.URL.createObjectURL(blob);
link.click();
-
Sadly still a lot of IE9 about and caniuse.com/#search=blob.– Keith CCommented May 24, 2015 at 13:07
-
IE8/9 is < 5%. Hardly anything to worry about much. Anyone not running IE 10 should expect to have increasingly worse performance on all kinds of sites because developers are starting to use Chrome/Firefox for any serious debugging (because they have superior debugging tools, F12 is a joke). It's still crazy that I have to test all my code three times-- once for standards, once for IE 8, and once for IE 9. See something wrong with that? Still supporting IE <10 means tripling QA work.– sfdcfox ♦Commented May 24, 2015 at 13:55
-
@sfdcfox I'd love to agree with you. But recently had the bad experience of a customer worth $ to us who runs IE9 on all desktops i.e. IE9 was 100% of the deal. Had to spend several days adding fallback code for them for a Salesforce hosted Angular app we deliver. But as I understand it in Jan 2016 Microsoft will push their customers to IE11 so we may finally be dealing with all modern browser APIs.– Keith CCommented May 24, 2015 at 15:55
-
@KeithC That's true for a minority of organizations that refuse to update/upgrade. It's still largely circumstantial, though. If I were releasing an app on the AppExchange, for example, I would require a standards-compliant browser in order to reduce support and development costs. Unless, of course, I was writing hospital-bound software, since hospitals are notorious for using older tech and software. Of course, if the $ is still there, then supporting older software can be a thing.– sfdcfox ♦Commented May 24, 2015 at 20:15