Carrierwave File Upload with Different File Types

Carrierwave file upload with different file types

You should try using like this

class FileUploader < CarrierWave::Uploader::Base  
include CarrierWave::MiniMagick

process :process_image, if: :image?
process :process_pdf, if: :pdf?

protected

def image?(new_file)
new_file.content_type.start_with? 'image'
end

def pdf?(new_file)
new_file.content_type.start_with? 'application'
end

def process_image
# I process image here
end

def process_pdf
# I process pdf here
end
end

Carrierwave different size limits for different file types

you can try this way

class Product < ActiveRecord::Base 
mount_uploader :inv_file, InvFileUploader

validate :file_size

def file_size
extn = file.file.extension.downcase
size = file.file.size.to_f
if ["png", "jpg", "jpeg"].include?(extn) && size > 5.megabytes.to_f
errors.add(:file, "You cannot upload an image file greater than 5MB")
elsif (extn == "pdf") && size > 20.megabytes.to_f
errors.add(:file, "You cannot upload an pdf file greater than 20MB")
else
errors.add(:file, "You cannot upload a file greater than 25MB")
end
end
end

Mixed file types with CarrierWave

According to the Carrierwave Docs you can do the conditional processing:

version :medium, :if => :image? do
process :resize_to_fit => [300, 300]
end

protected

def image?(new_file)
new_file.content_type.include? 'image'
end

Actually more full answer I found here

CarrierWave: create 1 uploader for multiple types of files

I came across this, and it looks like an example of how to solve this problem: https://gist.github.com/995663.

The uploader first gets loaded when you call mount_uploader, at which point things like if image? or elsif video? won't work, because there is no file to upload defined yet. You'll need the methods to be called when the class is instantiated instead.

What the link I gave above does, is rewrite the process method, so that it takes a list of file extensions, and processes only if your file matches one of those extensions

# create a new "process_extensions" method.  It is like "process", except
# it takes an array of extensions as the first parameter, and registers
# a trampoline method which checks the extension before invocation
def self.process_extensions(*args)
extensions = args.shift
args.each do |arg|
if arg.is_a?(Hash)
arg.each do |method, args|
processors.push([:process_trampoline, [extensions, method, args]])
end
else
processors.push([:process_trampoline, [extensions, arg, []]])
end
end
end

# our trampoline method which only performs processing if the extension matches
def process_trampoline(extensions, method, args)
extension = File.extname(original_filename).downcase
extension = extension[1..-1] if extension[0,1] == '.'
self.send(method, *args) if extensions.include?(extension)
end

You can then use this to call what used to be process

IMAGE_EXTENSIONS = %w(jpg jpeg gif png)
DOCUMENT_EXTENSIONS = %(exe pdf doc docm xls)
def extension_white_list
IMAGE_EXTENSIONS + DOCUMENT_EXTENSIONS
end

process_extensions IMAGE_EXTENSIONS, :resize_to_fit => [1024, 768]

For versions, there's a page on the carrierwave wiki that allows you to conditionally process versions, if you're on >0.5.4. https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Do-conditional-processing. You'll have to change the version code to look like this:

version :big, :if => :image? do
process :resize_to_limit => [160, 100]
end

protected
def image?(new_file)
new_file.content_type.include? 'image'
end

Carrierwave how to get the file extension

Determine file extension (I suppose a name for mounted uploader is 'file'):

file = my_model.file.url
extension = my_model.file.file.extension.downcase

Then prepare mime and disposition vars:

disposition = 'attachment'
mime = MIME::Types.type_for(file).first.content_type

if %w{jpg png jpg gif bmp}.include?(extension) or extension == "pdf"
disposition = 'inline'
end

(add other extensions if you want).

And then send the file:

send_file file, :type => mime, :disposition => disposition

CarrierWave multiple file types validation with single uploader

I ran into the exact same use case:

In Asset.rb
Validate the format of the filename

validates :asset_file,
format:{
with: %r{\.(pdf|doc|png)$}i, message: "Wrong file format"
}

Use a regular expression to test the file name:

Here you can play with the regex:
http://rubular.com/r/Z3roRDDXAf

Hope this helps!

How to upload multiple files with carrierwave and create multiple records

You have to implement your requirement with has_many relation.
To save multiple record you may have parent child relationship just like below.

Ex :-
Post is a parent table and save its images as post_attachments which is child table

class Post < ActiveRecord::Base
has_many :post_attachments
accepts_nested_attributes_for :post_attachments
end

class PostAttachment < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
belongs_to :post
end

this is how you can achieve to save multiple files for parent record

For more detail see this Article which may be helpful to you.

https://kolosek.com/carrierwave-upload-multiple-images/

rails carrierwave, upload multiple files

you have two options

first option use a json field in your db to upload the pictures. Not all db allow this, for example some mysql2 db do not allow you to use the json type field in your db. Also using this is not the ruby on rails best practice

rails carrierwave, one user has many image

The other option is from this solution, you will not use a JSON field in the db, but create a separate table

Rails 4 multiple image or file upload using carrierwave

now please tell me why you do not want to use either this solutions, because I implemented both and I will try to help you

Also from your question it is not clear the issue you are having



Related Topics



Leave a reply



Submit