简体   繁体   English

将Carrierwave附件编码到Rails中的base64

[英]Encode Carrierwave attachment to base64 in Rails

I am using the Carrierwave gem to upload attachments to my model. 我正在使用Carrierwave gem将附件上传到我的模型。 I added elasticsearch with the mapper attachments plugin to allow for full text search of the attachments. 我使用mapper附件插件添加了elasticsearch,以允许对附件进行全文搜索。

Carrierwave and elasticsearch work fine, but in order to get the full text search working I need to encode the attachment as base64. Carrierwave和elasticsearch工作正常,但为了使全文搜索工作,我需要将附件编码为base64。

I have followed this tutorial ( http://rny.io/rails/elasticsearch/2013/08/05/full-text-search-for-attachments-with-rails-and-elasticsearch.html ) but I assume there has been some changes to either Rails or Carrierwave as I can't get it to work. 我已经按照本教程( http://rny.io/rails/elasticsearch/2013/08/05/full-text-search-for-attachments-with-rails-and-elasticsearch.html )进行了操作,但我认为已有对Rails或Carrierwave进行一些更改,因为我无法使其工作。 Specifically, when I am trying to encode the attachment as base64, I get the following Type error: 具体来说,当我尝试将附件编码为base64时,我收到以下类型错误:

no implicit conversion of CarrierWave::SanitizedFile into String 没有将CarrierWave :: SanitizedFile隐式转换为String

The error is in the following line of the model: 错误位于模型的以下行中:

File.open(Base64.encode64(File.read(document.file)))

If I replace the path with a url to an actual file it works fine. 如果我用一个url将路径替换为实际文件,它可以正常工作。

I have searched SO and the only relevant answer I can find gives me the same error: Carrierwave encode file to base64 as process 我搜索了SO,我能找到的唯一相关答案给了我同样的错误: Carrierwave编码文件到base64作为进程

I am a complete rails newbie and hopefully this is something that's obvious to everyone except me, but we're all beginners at first, right? 我是一个完整的铁杆新手,希望这对我以外的每个人都很明显,但我们一开始都是初学者,对吧?

Thanks! 谢谢!

CarrierWave's read method returns the content of the file. CarrierWave的read方法返回文件的内容。 So assuming Document is your model and file is your uploader attribute, this should work: 因此,假设Document是您的模型, file是您的上传器属性,这应该工作:

Base64.encode64(document.file.read)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM