Send xlsx file to front end - AngularJS

718 Views Asked by At

I want to send xlsx file created by rubyXL gem to front end, which is AngularJS. When I save worksheet in a stream and send it by send_data method, I get the data, but I can't open the created document because it is broken.

workbook = RubyXL::Workbook.new
worksheet = workbook[0]
worksheet.sheet_name = 'Average of Team'
worksheet.add_cell(0, 0, 'A1')
buffer = workbook.sream
send_data buffer

I am able to save the data to disk. But I can't be able to send it to client side. so, that I can access the data from angularjs.

workbook = RubyXL::Workbook.new
worksheet = workbook[0]
worksheet.sheet_name = 'Average of Team'
worksheet.add_cell(0, 0, 'A1')
path = "#{Rails.root}/tmp/#{Time.now.strftime('%Y%m%d%H%M%S%L')}.xlsx"
workbook.write(path)
send_file path

I don't want to save the workbook on cloud and send a link to the client side. What is the best solution of my problem? Where I make mistake?

UPD: It seems when I trying to send saved file, I delete file before sending completes.

UPD: When I am sending data, I have problems with encoding data. I tried to use string.bytes.to_a.pack("C*"), string.force_encoding('binary'). But it didn't help me.

1

There are 1 best solutions below

0
On

I think that the problem is that there is incompatibility between ruby and js libs. One possible solution will be running front-end library on the back-end, in order to deal with the file with the same libs. It's possible to do using V8 js engine https://github.com/cowboyd/therubyracer

But I am not 100% sure that my solution is unique and the best one :) I had a similar problem that I've solved in such way.