How to include image in CSV

In a Rails application, an administrator can export user data in csv format. Each user of my application has a photo of his profile .... my client wants to include a user photo in a CSV file. I do not know how to do that. can someone please help me ....

I am using quickcsv gem and here is some kind of 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 
+4
source share
5 answers

Saving the actual photo in a CSV file is technically possible, but highly discouraged. CSV is simply not designed for this kind of work. You obviously can't just paste binary image data into an ASCII text CSV file. One could use Base-64 encoding to convert 8-bit binary data to 7-bit text, and then save this in one of the fields in the CSV file. (It will also expand the storage volume needed by about 20%).

But then what software could decode it? You will need to write some other software to convert the images back to the other end in order to defeat the target. In addition, each line of the CSV file will be massive and will probably cause import problems.

You would be much better off exporting your profile photos as a PNG set and save the file name in a CSV file against each entry.

+11
source

CSV is plain text. It is not possible to include graphic data if both the generator and the reader do not agree to a format such as base64 encoded PNG.

+4
source

You can try add

  • links to image files
  • base64 single-line string
+1
source

CSV (comma separated values) is not a format suitable for binary data. A general rule, but to save binary data to a text file is to convert to a format corresponding to text files, such as Base64, or a text representation of raw data in Hex.

+1
source

Do you consider using the SVG format? You should be able to embed xml in csv.

0
source

Source: https://habr.com/ru/post/1302356/


All Articles