Saving .xlsx files in SQL Database through Rails controller

Saving .xlsx files in SQL Database through Rails controller



I'm creating a .xlsx file using 'axlsx_rails' gem. I'm successfully able to save the file to the browser using:



send_data(xlsx.to_stream.read, type: "application/xlsx", filename: filename)


send_data(xlsx.to_stream.read, type: "application/xlsx", filename: filename)



However - I want to be able to save this file in the database, at the same time the user downloads it, to be able to access the file later.



I can't figure out how to do this - saving as xlsx.to_stream.read doesn't work, other variations just give me strings - I want to save it as an actual .xlsx file.



How do I do this?



Edit:



This is what I currently have:


class ReportsUploader < CarrierWave::Uploader::Base
def store_dir
"reports"
end
end



Then in the model:


class Report < ApplicationRecord

mount_uploader :file, ReportsUploader
end



Then when I save the actual report:


send_data(report.xlsx.to_stream.read, type: "application/xlsx", filename: report.filename)



That causes the download of the file for the user



Then I do


Report.create!(file: Rails.root.join('user_information_report.xlsx'))



Edit: This actually works!





You can use send_file method to download the file and either paperclip or carrierwave to save file to database.
– Kedarnag Mukanahallipatna
Aug 23 at 16:31


send_file


paperclip


carrierwave





@KedarnagMukanahallipatna thanks! I'm trying carrierwave now but can't seem to get the actual file correct. It just seems to be strings - do I just send the xlsx variable to carrierwave? Or something like xlsx.to_stream.read?
– yungMucey
Aug 23 at 16:46



carrierwave


xlsx


carrierwave


xlsx.to_stream.read





You will need to actually serialize the package into a real file and store that file externally. I would not recommend you trying to store the raw string data into the database. That being said why store it at all axlsx is a fairly efficient xlsx generation gem and building the spreadsheets on the fly is most likely a better choice.
– engineersmnky
Aug 23 at 16:55


axlsx


xlsx





@engineersmnky I am actually saving the spreadsheet already - but I'm also required to save the same file to the database. How do I store that file externally and then grab it to save it in the DB?
– yungMucey
Aug 23 at 16:56





Looks like you can use serialize to make a file out of the xlsx. If you wanted to, you could write this to a temporary directory then read it in to your database. I wouldn't suggest that though. Storing binary data in your DB is usually not a good choice. From the code: p = Axlsx::Package.new # ......add cool stuff to your workbook...... p.serialize("example.xlsx")
– bkimble
Aug 23 at 16:57










By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)