Below is the result. It supports IE 10+ (potentially 9 haven't tested), FireFox, and Chrome.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | angular.module( 'FileDownload' ) .service( 'SaveFileService' , [ function () { this .Save = function (data, filename, mimeType) { var blob = new Blob([data.join( '\n' )], {type: mimeType}); if (/\bMSIE\b|\bTrident\b|\bEdge\b/.test(navigator.userAgent)) { window.navigator.msSaveOrOpenBlob(blob, filename); } else { var url = window.URL || window.webkitURL, event = document.createEvent( "MouseEvents" ); link.href = url.createObjectURL(blob); link.download = filename; event.initEvent( "click" , true , false ); link.dispatchEvent(event); } }; }]); |
To use the service, just inject the service into the controller and call the Save function w/ the needed parameters.
UPDATE 2015-09-28: Per Kevin's feedback, I have updated the regular expression to catch the Edge browser (so much for Microsoft making a standards evergreen browser). I have verified that the above change works as advertised.
2 comments:
I'm using this in my project, tested it for IE10, IE11, Firefox, Chrome and Opera. It doesn't work anymore for Edge but you just need to change the regex to /\bMSIE\b|\bTrident\b|\bEdge\b/
Kevin: I successfully tested your change in my demo application and updated my example accordingly. Thanks for the feedback!
Post a Comment