我如何将图像包含到 CSV 中 [英] How can i include image into CSV

查看:52
本文介绍了我如何将图像包含到 CSV 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Rails 应用程序中,管理员可以将用户数据导出为 csv 格式.我的应用程序中的每个用户都有他们的个人资料照片......我的客户想要将用户照片包含到 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....

我正在使用fastercsv gem,这里有一些我的控制器代码供参考

i am using fastercsv gem and here is some my controller code for reference

在我的控制器中:

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

推荐答案

将实际照片保存在 CSV 文件中在技术上是可行的,但非常不明智.CSV 根本不适合这种工作.您显然不能简单地将二进制图像数据嵌入到基于 ASCII 文本的 CSV 文件中.可以使用 Base-64 编码 将 8 位二进制数据转换为 7-位文本,然后将其存储在 CSV 文件的字段之一中.(这也会将所需的存储空间扩大约 20%).

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%).

但是什么软件可以解码呢?您将不得不编写一些其他软件来将图像转换回另一端,这将违背目的.此外,CSV 文件的每一行都会很大,可能会导致导入问题.

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.

最好将个人资料照片导出为一组 PNG,并将文件名保存在每个记录的 CSV 文件中.

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 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆