1

I am using Carrierwave for file uploading and have got the following form, which allows me to submit several files:

<%= form_tag load_patterns_contacts_path, multipart: true, multiple: true do %>
  <%= file_field_tag 'qqfile[]', id: "upload_pattern", multiple: true %>
  <%= submit_tag "Load", id: "save_pattern", :class => 'btn btn-primary btn-success', multiple: true%>
<% end %>

Here is the code in my controller, which load submited files to the server:

 @uploader = EmailPatternsUploader.new

  params[:qqfile].each do |p|
    tempfile = open(p.original_filename)
      puts tempfile
    @uploader.store!(tempfile)
  end

  redirect_to contacts_path
  flash[:success] = "Uploaded successfully."

It works fine, if filename looks like "text.xlsx", "image.jpg" etc. But if it is contains special symbols like "_partial.html.erb" then I have got Errno:ENOENT (No such file or directory - _partial.html.erb)

I have tried to add

CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\_\-\+]/ 

in my carrierwave.rb initializer, but it gives no result.

Thanks in advance for help!

UPDATE:

I have understood, that the problem not in special symbol "_", but in the fact, that samples I am trying to upload contains two dots ("."). I think I need to modify regular expression in order to avoid two dots

UPDATE:

I am sorry for the last comments. I have understood, that the matter not in special symbols at all and not in a name of file. The problem that i can upload files only from {Rails.root} path. If I choose another directory, I have got aforementioned error and cannot upload a file. How can I configure Carrierwave path directory?

Mihail Davydenkov
  • 1,861
  • 2
  • 19
  • 33

1 Answers1

0

Finally find an answer on my question.

The error was in these strings of code:

params[:qqfile].each do |p|
  tempfile = open(p.original_filename)
    puts tempfile
  @uploader.store!(tempfile)
end

I have understood, that I need to pass an object ActionDispatch::Http::UploadedFile in Carrierwave store! method. Thats why the mentioned above code shall be the following:

params[:qqfile].each do |p|
  puts p.original_filename
    puts p
  @uploader.store!(p)
end

==================================================================================

Hope someone find this solution for multiple file uploading with Carrierwave and without JQuery useful.

1) Create an uploader, using Carrierwave.

    rails g uploader EmailPatterns

2) Create a custom action for your controller (watch Railscast#35 and Railscast#38 to make it clear) and put there something like this (load_patterns in my case):

    def load_patterns
      @uploader = EmailPatternsUploader.new
        params[:qqfile].each {|p| @uploader.store!(p)}
      redirect_to contacts_path
      flash[:success] = "Uploaded successfully"
    end

To make it work you need to specify custom route(config/routes.rb) for your action:

  resources :contacts do
    collection { post :load_patterns}
  end

and to create a form, where you will get params with your uploading files (see p.3)

3) Create form, where you need to specify the option multiple:true in order to allow user to select several files to load (param name with [ ] is a necessary requirement, because we are loading several files) :

    <%= form_tag load_patterns_contacts_path, multipart: true, multiple: true do %>
       <%= file_field_tag 'qqfile[]', id: "upload_pattern", multiple: true %>
       <%= submit_tag "Load",  id: "save_pattern", :class => 'btn btn-primary btn-success', multiple: true%>
    <% end %>

Then your custom action will work.

Mihail Davydenkov
  • 1,861
  • 2
  • 19
  • 33