javascript - Download Excel file from server and save on client -
i have javascript app , api creates excel file , returns byte array following headers:
content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet content-disposition:attachment; filename=list.xlsx when go excel resource api url i'm prompted download excel file. if so, downloads fine , opens in excel. good.
now problem:
what don't want expose api url in user's browser window, goal to:
- download excel file via ajax xmlhttprequest
- store contents (byte array) in blob
- create data uri blob
- open data uri in popup, prompts user download excel file
what have this:
it downloads file, when try open file, excel doesn't recognize valid excel file.
// "data" contents server var reader = new filereader(); var blob = new blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); reader.readasdataurl(blob); reader.onloadend = function (e) { window.open(reader.result, 'excel', 'width=20,height=10,toolbar=0,menubar=0,scrollbars=no', '_blank'); }
i got working. had add following xmlhttprequest object:
responsetype: 'arraybuffer' but doesn't work in ie, because ie cannot open data uris. not ie11.
anyway found a great library called filesaver.js handles saving files major browsers (including ie10+)
Comments
Post a Comment