Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i include image into CSV

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
like image 256
palani Avatar asked Feb 25 '10 11:02

palani


People also ask

Can you put an image in a CSV?

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.

How do I convert an image to a CSV file?

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.


3 Answers

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.

like image 100
gavinb Avatar answered Sep 28 '22 07:09

gavinb


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.

like image 42
Ignacio Vazquez-Abrams Avatar answered Sep 28 '22 06:09

Ignacio Vazquez-Abrams


You could try adding

  • links to image files
  • single line base64 encoded string
like image 39
YOU Avatar answered Sep 28 '22 07:09

YOU