Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
814 views
in Technique[技术] by (71.8m points)

ruby on rails - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...