angularjs - Serving excel sheet (.xls) to browser for download using rails & angular -
i using spreadsheet gem generate .xls file. after writing file, trying send client browser download.
below code in rails
workbook = spreadsheet::workbook.new # constructed data file = "/path/to/file/sheet.xls" workbook.write file send_file file
this file when opened contains expected data in ideal format.
below code in js:
customrestservice.custom_post("report",{report_data: angular.tojson($scope.report_data)},"download_xls",{}).then (data)-> if data hiddenelement = document.createelement('a') angular.element(document.body).append(hiddenelement) hiddenelement.href = 'data:attachment/xls,' + encodeuri(data) hiddenelement.target = '_blank' hiddenelement.download = "report.xls" hiddenelement.click() hiddenelement.remove()
but file getting downloaded in browser contains junk data. tried multiple solutions below:
- using send_data, instead of send_file
- generated xls data , wrote stringio object directly download
- constructed blob object in js, type "application/vnd.ms-excel" , trying download it.
all attempts failed, missing something. suggestions welcome.
filename = "/path/to/file/sheet.xls" tempfile = tempfile.new(filename) workbook = spreadsheet::workbook.new ... workbook.write(tempfile.path) send_file tempfile.path, :filename => filename
Comments
Post a Comment