简体   繁体   中英

Rails 5 and Carrierwave not uploading docx

I have a Rails 5 app. I'm using Carrierwave for file uploads. I am able to successfully upload in development and production environments, PDFs, JPGs, PNGs. However, doc, docx, xls, xlsx do not upload. 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).

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:

include CarrierWave::RMagick
storage :fog

I am able to upload pdf, png and jpg formats.

However, when I upload a docx file I get this error message:

Upfile translation missing: en.errors.messages.rmagick_processing_error

To fix this error message, I add this 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:

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. 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?). 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. 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):

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. 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". So RMagick can not process documents (doc, docx, xls, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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