$scope.saveToPc = function (data, filename) { if (!data) { console.error('No data'); return; } if (!filename) { filename = 'download.json'; } if (typeof data === 'object') { data = JSON.stringify(data, undefined, 2); } var blob = new Blob([data], {type: 'text/json'}); // FOR IE: if (window.navigator && window.navigator.msSaveOrOpenBlob) { window.navigator.msSaveOrOpenBlob(blob, filename); } else{ var e = document.createEvent('MouseEvents'), a = document.createElement('a'); a.download = filename; a.href = window.URL.createObjectURL(blob); a.dataset.downloadurl = ['text/json', a.download, a.href].join(':'); e.initEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); a.dispatchEvent(e); } };
The solution is shamelessly copied from http://bgrins.imtqy.com/devtools-snippets/#console-save
Edit Thanks to @ufk, the deprecated initMouseEvent method has initMouseEvent to initEvent . I do not know why it does not work in MSIE 11, possibly due to security restrictions. And Microsoft Edge has a new way to set all the properties of a synthetic event, but I have not tested it.
hgoebl
source share