In my Rails application Admin can export the user data into csv format. Every user in my application has their profile photo.... my client wants to include the user photo into the CSV file .. I have not how to do this . can any one please help me....
i am using fastercsv gem and here is some my controller code for reference
In my Controller :
require 'fastercsv'
def getcsv
entries = User.find(:all)
csv_string = FasterCSV.generate do |csv|
csv << ["first_name","last_name","username","address","photo" ]
entries.each do |e|
csv << [e.first_name,e.last_name,e.username,e.address,e.photo.url]
end
end
send_data csv_string,:type=>'text/csv;charset=iso-8859-1; header=present', :filename=>"entries.csv",:disposition => 'attachment'
end
Adding images to a CSV file requires that your images are available on the internet and that you have a public URL for them, which you can add to your CSV file. If the images are already on a website, you can just use their existing URL.
To convert a JPG image to CSV format, simply drag and drop the JPG file into the data upload area, select the 'OCR' option, and click the 'Convert' button. You'll get an CSV spreadsheet populated with data from the JPG file.
Saving the actual photo in a CSV file is technically possible, but very ill-advised. CSV is simply not intended for that sort of job. You obviously cannot simply embed the binary image data into the ASCII text-based CSV file. It would be possible to use Base-64 encoding to convert the 8-bit binary data into 7-bit text, and then store this in one of the fields in your CSV file. (This will also expand the storage required by about 20%).
But then what software could decode this? You would have to write some other software to convert the images back on the other end, which would defeat the purpose. Furthermore, each line of the CSV file would be massive, and probably cause problems on importing.
You would be much better off exporting the profile photos as a set of PNGs, and save the filename in the CSV file against each record.
CSV is plain text. There's no way to include graphic data unless both the generator and the reader agree on a format, such as base64-encoded PNG.
You could try adding
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With