使用 ruby 将数据库中上传的文件转换为 xml 文件中的 cdata

Convert files uploaded in database into cdata in an xml file with ruby

我将文件存储在 document tablepaperclip 中。我的文件是 word,csv,xlsxpdf。 我想从数据库中读取这些记录并将它们转换为 cdata 并将它们放在名为 Attachment.

的 xml 标签中

这是我期待的输出:

<Attachment ContentType="xlsx" Extension="xlsx" Description="TEST2.xlsx"><![CDATA[UEsDBBQABgAIAAAAIQBxDjkrcAEAAKAFAAATANsBW0NvbnRlbnRfVHlwZXNdLnhtbCCi1wEooAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA..

我的文件查询时是这样的:

#<Paperclip::Attachment:0x000000049dfe28 @name=:picture, @name_string="picture", @instance=#<Document id: 225, picture_file_name: "data", picture_content_type: "application/vnd.openxmlformats-officedocument.word...", picture_file_size: 10001, picture_updated_at: "2018-02-07 20:14:18", ece_id: 242, created_at: "2018-02-07 20:14:18", updated_at: "2018-02-07 20:14:18", xml_file_name: nil, xml_content_type: nil, xml_file_size: nil, xml_updated_at: nil, whodunnit: nil, document_type: "Attachment">, @options={:convert_options=>{}, :default_style=>:original, :default_url=>"/:attachment/:style/missing.png", :escape_url=>true, :restricted_characters=>/[&$+,\/:;=?@<>\[\]\{\}\|\\^~%# ]/, :filename_cleaner=>nil, :hash_data=>":class/:attachment/:id/:style/:updated_at", :hash_digest=>"SHA1", :interpolator=>Paperclip::Interpolations, :only_process=>[], :path=>":rails_root/public:url", :preserve_files=>false, :processors=>[:thumbnail], :source_file_options=>{}, :storage=>:filesystem, :styles=>{}, :url=>"/system/:class/:attachment/:id_partition/:style/:filename", :url_generator=>Paperclip::UrlGenerator, :use_default_time_zone=>true, :use_timestamp=>true, :whiny=>true, :validate_media_type=>true, :check_validity_before_processing=>true}, @post_processing=true, @queued_for_delete=[], @queued_for_write={}, @errors={}, @dirty=false, @interpolator=Paperclip::Interpolations, @url_generator=#<Paperclip::UrlGenerator:0x000000049dfd10 @attachment=#<Paperclip::Attachment:0x000000049dfe28 ...>>, @source_file_options={}, @whiny=true> 

我正在使用 nokogiri 生成 xml 标签。

你有什么想法吗?提前致谢

您可以像这样使用回形针读取文件内容:

file_content = Paperclip.io_adapters.for(attachment.file).read

使用 Nokogiri 将 CDATA XML 标记写入 XML 文档的方法有很多种,具体取决于您构建文档的方式,这里是 cheat sheet:

doc.create_cdata(file_content)

但是,您应该使用 base64 编码对文件内容进行编码,因为文件内容是二进制的,并且它可能包含 XML 文档中不允许的字符。

这里是:

require 'base64' # if not already required
Base64.encode64(file_content)

总而言之,这是一个伪片段:

file_content = Paperclip.io_adapters.for(attachment.file).read
doc.create_cdata(Base64.encode64(file_content))

您还应该考虑使用 Base64 元素而不是 CDATA,查看 this and this 答案(以及其他关于同一问题的答案)。