How to Set a File Upload Programmatically Using Paperclip

How to set a file upload programmatically using Paperclip

What do you mean by programmatically? You can set up a method that will take a file path along the lines of

my_model_instance = MyModel.new
file = File.open(file_path)
my_model_instance.attachment = file
file.close
my_model_instance.save!

#attachment comes from our Paperclip declaration in our model. In this case, our model looks like

class MyModel < ActiveRecord::Base
has_attached_file :attachment
end

We've done things similar to this when bootstrapping a project.

Paperclip Audio file upload

This will work for any type of the file

validates_attachment_content_type :avatar, :content_type => /.*/

You can also discover exact content type of the file with command

file -i path/to/file # or 
file --mime-type path/to/file

I have run in on MP3 file and it returned

audio/mpeg

So if you want to validate only some set of content types you can add 'audio/mpeg' to the list

validates_attachment_content_type :avatar, :content_type => [ ..., 'audio/mpeg', ...]

Set Paperclip image field to url without uploading

Probably not possible, so I just ended up using the default_url to display a generic image for my sample data.

paperclip upload yml file

Use the debugger to check params[:mailing_search] in your controller.

I suspect params[:mailing_search][:search_config].content_type will be application/octet-stream since a .yml file is treated as binary.

Since you don't allow application/octet-stream as a valid content type, that's why you get the error.

When you try through the console the content type is not being overridden by the browser, hence why it works.

In your controller, before building @search, you can reset the content type in the params to the file's MIME type using:

params[:mailing_search][:search_config].content_type = MIME::Types.type_for(params[:mailing_search][:search_config].original_filename).first.content_type

This should preserve text/x-yaml as the content type for the file which you allow as valid.

Uploading thousands of images with Paperclip to S3

You can configure your app to use Amazon S3 for paperclip storage in development (see my example) and upload the files using a rake task like this:

Lets's say your folder of images was in your_app_folder/public/images, you can create a rake task similar to this.

namespace :images do
desc "Upload images."
task :create => :environment do
@images = Dir["#{RAILS_ROOT}/public/images/*.*"]
for image in @images
MyModel.create(:image => File.open(image))
end
end
end

With Rails Paperclip gem, how to temporarily save the uploaded file if the model is invalid

Define a before_save method that checks if the object is valid,
if not save the file to disk, give it a unique name (create some hash)

Put that in the form you send back in a hidden field

Delete the Upload field in the form

Now in the else branch of the before_save method check if there was a hidden_field previous_upload or however you name it
If there is load the picture and assign it to the paperclip attribute, it can figure out the rest

attr_accessor :previous_upload

def before_save
if valid?
if previous_upload
paperclip_file = #Load paperclip_file from /tmp
else
previous_upload = nil
end
else
previous_upload = "Some unique key for each upload like ip and time or such"
# Save paperclip_file with name previous_upload to /tmp
end
end


Related Topics



Leave a reply



Submit