Ruby-on-rails 我如何将图像包含到 CSV 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2333522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i include image into CSV
提问by palani
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....
在我的 Rails 应用程序中,管理员可以将用户数据导出为 csv 格式。我的应用程序中的每个用户都有他们的个人资料照片......我的客户想要将用户照片包含到 CSV 文件中......我不知道如何做到这一点。谁能帮帮我吗....
i am using fastercsv gem and here is some my controller code for reference
我正在使用 fastcsv gem,这是我的一些控制器代码以供参考
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
回答by gavinb
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 encodingto 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 文件中在技术上是可行的,但非常不明智。CSV 根本不适合这种工作。您显然不能简单地将二进制图像数据嵌入到基于 ASCII 文本的 CSV 文件中。可以使用Base-64 编码将 8 位二进制数据转换为 7 位文本,然后将其存储在 CSV 文件的字段之一中。(这也会将所需的存储空间扩大约 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.
但是什么软件可以解码呢?您将不得不编写一些其他软件来将图像转换回另一端,这将违背目的。此外,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.
您最好将个人资料照片导出为一组 PNG,并将文件名保存在 CSV 文件中,以针对每条记录。
回答by Ignacio Vazquez-Abrams
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.
CSV 是纯文本。除非生成器和阅读器都同意一种格式,例如 base64 编码的 PNG,否则无法包含图形数据。
回答by YOU
You could try adding
你可以尝试添加
- links to image files
- single line base64 encoded string
- 图像文件链接
- 单行 base64 编码字符串
回答by Tim Jarvis
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.
CSV(逗号分隔值)不是适合二进制数据的格式,但将二进制数据保存到文本文件的一般经验法则是将其转换为适合文本文件的格式,例如 Base64 或文本表示以十六进制表示的原始数据。

