views:

202

answers:

4

Hi

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
+3  A: 

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.

Ignacio Vazquez-Abrams
Oh ok.... IS that no way to include the picture into that?
palani
Figure out what the reader can take, then use that.
Ignacio Vazquez-Abrams
Oh ok thanks for your help
palani
+1  A: 

You could try adding

  • links to image files
  • single line base64 encoded string
S.Mark
How should i do that... can you please give more specific
palani
+1  A: 

CSV (comma separated values) is not a format suitable for binary data, the general rule of thumb though for saving binary data to a text file is to convert it to a format that does suit text files, something like Base64, or a textual representation of the raw data in Hex.

Tim Jarvis
+7  A: 

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.

gavinb