I've been struggling to save an Excel file using Javascript, but I'm facing compatibility issues with different browsers. I initially tried using BASE64 along with data URL, which worked well in Chrome and Firefox but failed in IE and Safari.
newlink = document.createElement('a');
newlink.setAttribute('href', 'data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,' + response.data);
newlink.setAttribute('download', 'test.xlsx');
newlink.click();
I also experimented with the Blob method, but it seems to have trouble handling binary files like PDFs, Excels, and MS Word documents.
var report = response;
var str = atob(report.data);
var blob = new Blob([str], { type: report.type});
saveAs(blob, 'report.csv');
This approach works fine for CSV files, but when trying the same with Excel or Word files, the downloaded file becomes corrupted.
I am using AngularJS for REST calls and implementing token authentication through headers. Simply opening a URL won't work due to authorization restrictions. I have tried both $resource and $http methods with no success. Interestingly, the REST API functions properly because when I access the URL directly in my browser without authentication, the file downloads correctly. The issue seems to arise during Blob creation.
If anyone has solutions or suggestions on how to resolve this dilemma, your input would be greatly appreciated.