简体   繁体   English

Rails 5和Carrierwave没有上传docx

[英]Rails 5 and Carrierwave not uploading docx

I have a Rails 5 app. 我有一个Rails 5应用程序。 I'm using Carrierwave for file uploads. 我正在使用Carrierwave进行文件上传。 I am able to successfully upload in development and production environments, PDFs, JPGs, PNGs. 我能够在开发和生产环境中成功上传PDF,JPG,PNG。 However, doc, docx, xls, xlsx do not upload. 但是,doc,docx,xl​​s,xlsx不会上传。 The error message I am getting is: 我收到的错误消息是:

Upfile Failed to manipulate with rmagick, maybe it is not an image?

Upfile is the name of my database column (stands for uploadedfile). Upfile是我的数据库列的名称(代表uploadfile)。

I will walk you through my setup and the steps I have taken to fix this issue: 我将指导您完成设置以及为解决此问题而采取的步骤:

I have these gems: 我有这些宝石:

gem "rmagick"
gem 'carrierwave', '~> 1.0'
gem 'fog'

In my CarrierWave uploader file, I call: 在我的CarrierWave上传器文件中,我调用:

include CarrierWave::RMagick
storage :fog

I am able to upload pdf, png and jpg formats. 我可以上传pdf,png和jpg格式。

However, when I upload a docx file I get this error message: 但是,当我上传docx文件时,出现以下错误消息:

Upfile translation missing: en.errors.messages.rmagick_processing_error

To fix this error message, I add this gem: 要修复此错误消息,我添加了以下gem:

gem 'carrierwave-i18n'

Adding this gem solves the problem, but then when I try to upload a docx file, I receive a different error message: 添加此gem可以解决问题,但是当我尝试上传docx文件时,会收到另一条错误消息:

Upfile Failed to manipulate with rmagick, maybe it is not an image?

So I proceed to debug this error. 因此,我继续调试此错误。 I consider that maybe my ImageMagick library is incomplete and missing a delegate that allows me to recognize a docx file. 我认为我的ImageMagick库可能不完整,并且缺少允许我识别docx文件的委托。 I look at the delegates by running: 我通过运行查看代表:

convert -list configure

The terminal then shows me: 然后终端显示给我:

DELEGATES      bzlib mpeg freetype gslib jng jpeg lcms lzma png ps tiff xml zlib

From what I observe with the delegates, I have all the necessary components to read docx, doc, xlss, xls, etc. (I believe xml is the delegate that allows me to read a docx file, which is interpreted as zipped xml?). 从与委托人的观察中,我具有读取docx,doc,xlss,xls等的所有必要组件。(我相信xml是允许我读取docx文件的委托,该文件被解释为压缩的xml?) 。 This is where my search ends. 这是我的搜索结束的地方。 Please help me discover the solution from here? 请帮助我从这里发现解决方案?


[Answer Found] Thanks to Maruf, I was able to find the answer. [找到答案]感谢Maruf,我找到了答案。 The code I had in my PDF uploader file was (pardon the pdfuploader nomenclature – I was planning to use it solely for pdfs but then Word and Excel formats were introduced): 我在PDF上载器文件中的代码是(原谅pdfuploader命名法–我打算将其仅用于pdf,但随后引入了Word和Excel格式):

class PdfUploader < CarrierWave::Uploader::Base

  include CarrierWave::RMagick

  storage :fog

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  version :thumb do
    process resize_to_fit: [200, 200]
  end

  def extension_whitelist
    %w(pdf doc html docx jpg jpeg gif png txt mp3 xls xlsx)
  end

end

The answer to this issue was removing the version thumb code block. 该问题的答案是删除版本拇指代码块。 I was under the impression that :thumb was called optionally on images, but it seems that it ran on all uploads. 我的印象是:thumb在图像上被可选地调用,但似乎它在所有上载上都可以运行。 I should have known! 我早该知道! :/ :/

If you give the full code of the uploader file, it will easy to find the problem. 如果您提供了上传程序文件的完整代码,则很容易找到问题。 But I think you are processing image like resizing, cropping etc in that file using RMagick as you have included "include CarrierWave::RMagick". 但是我认为您正在使用RMagick处理该文件中的图像,例如调整大小,裁剪等,因为其中包含了“ include CarrierWave :: RMagick”。 So RMagick can not process documents (doc, docx, xls, xlsx). 因此,RMagick无法处理文档(doc,docx,xl​​s,xlsx)。 If you need same field to support all files then you can process images with condition by checking file format or extension otherwise you can use another uploader for document files. 如果您需要相同的字段来支持所有文件,则可以通过检查文件格式或扩展名来处理带有条件的图像,否则可以使用其他上传器来存储文档文件。

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

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