I have a Rails 4 app that uses Carrierwave and Fog to upload files to Amazon S3. The servers the app uses is Nginx and Thin.
The problem I'm having is when uploading files over 5mb.
I've tried searching everywhere for a way to get it working, I've tried CarrierwaveDirect and still can't get it to work.
Here is my Carrierwave config:
config.fog_directory = 'aws-bucket-name'
config.fog_public = false
config.fog_attributes = { multipart_chunk_size: 5242880 }
config.max_file_size = 200.megabytes # CarrierwaveDirect option
Here are the controller actions:
def new
#file = DownloadFile.new
end
def create
#file = DownloadFile.new
#file.file = params[:download_file][:file]
if #file.save
redirect_to downloads_path
else
render :new
end
end
And the form:
<%= form_for #file, multipart: true do |f| %> <!-- even tried with `multipart` wrapped in `html: {}` -->
<%= f.label :file %>
<%= f.file_field :file %>
<%= f.button t(:upload) %>
<% end %>
I can upload small files just fine, but when it comes to files that are over 5mb, the form just redisplays. No errors, no errors in the logs, nothing.
With Carrierwave Direct it looks like you would need to setup the form slightly differently (as it would need to set the url to one on S3, rather than on your own server). If you are dealing with large files this may be preferable as large files would otherwise tie up a process for an extended time. To make that work it looks like you'll want to use the direct_upload_form_for helper method instead of plain form_for, something like:
<%= direct_upload_form_for #uploader do |f| %>
<%= f.file_field :avatar %>
<%= f.submit %>
<% end %>
For a bit more detail on that, see: https://github.com/dwilkie/carrierwave_direct#rails
Related
I have created simple application form and i was trying to upload file and send it as email attachment. And i did it with this approach:
class ApplyController < ApplicationController
def prepare_email_content
ApplyMailer.with(params).apply.deliver_now
end
end
class ApplyMailer < ApplicationMailer
def apply
#company = Company.find(params[:company_id])
#candidate = params[:name]
#candidate_mail = params[:email]
#email = #company.email
attachments[params[:cv].original_filename] = params[:cv].read
mail to: #email, subject: 'Hello'
end
end
<h1>APPLY</h1>
<%= form_tag(apply_path, method: :post, multipart: true) do %>
<%= label_tag(:name, "First and last name:") %>
<%= text_field_tag(:name) %>
<%= label_tag(:email, "Email:") %>
<%= text_field_tag(:email) %>
<%= hidden_field_tag :company_id, params[:company_id] %>
<%= file_field_tag 'cv' %>
<%= submit_tag "Search", :name => nil %>
<% end %>
<%= link_to 'All offers', hello_path %>
Everything was working fine - i have tested application and it was fine. Then i have developed my application and when i come back to testing i started getting this error:
On the way i have installed some gems and updated few of them. I was checking out to commit where this feature was working and it is. But i'm not able to find any differences in my code. There were some changes in /.idea folder but i don't know if any of this files could trigger this issue.
I'm using rails 6.0.3 and ruby 2.5.8
EDIT
I can see that there is a problem inside called methods. Looks like it cannot find #sort_order value and it sets data value as nil. But i have no idea how to change working of this.
Replace the line you're attaching the file with
attachments["#{params[:cv].original_filename}"] = File.read(params[:cv].path)
Even though you're not saving the file, there is a still a local path to grab the file from.
Also, you should really consider passing the parameters to the mailer from the controller, rather than passing params in its entirety. That way if the information or format of the upload is incorrect, you can redirect back to the form and bypass sending the email.
I recently noticed that on my form, if I try to upload an empty file the page will get redirected to edit instead of create. If I try to upload the file with some text in it, the form will direct to create. I couldn't find any indication that this would happen as I create my object every time (it's never persisted). Is there an explanation behind this?
The code looks something like this:
//controller
def upload
#new_cool_file = CoolFile.new
end
//form in upload.html.erb
<%= form_for #new_cool_file, html: {role: "form"} do |f| %>
<div class="form-group">
<%= f.label :file %>
<%= f.file_field :file %>
</div>
<%= f.submit "Submit"%>
<% end %>
I think you do some wrong in the rails routes.rb file , can you please see tutorial about file upload.
https://richonrails.com/articles/allowing-file-uploads-with-carrierwave
http://railscasts.com/episodes/253-carrierwave-file-uploads
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?
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.
I came across file uploading problem in Rails. I found file_field :file helper, that can be used with form_for(#some_model). However, I cannot find any usage for this case, as those tags are used to create/edit some model, by mass assigning. There is, AFAIK, no possibility to treat fileupload as typical field ( See File upload won't work in Ruby on Rails 3 using Multipart Form ). In such a case, manual operation on uploaded file is required. So, why would someone even want to puts a fileupload as a part of model editing?
photo.rb
class Photo < ActiveRecord::Base
attr_accessible :name, :filename,
end
photo_form.html.erb
<%= form_for(#photo, :multipart => true) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.file_field :file %>
<%= f.submit %>
<% end %>
photos_controller.rb
def create
#photo = Photo.new(params[:photo])
line above fails, because theres no :file attribute. It must be handled before and manually removed from :params. Once more - is there any real usage for such tags?
I will will you an example how I am using it I think it explains itself good enough, I hope this helps
<%= form_for #item do |f|%>
<%= f.file_field :photo, accept: 'image/png,image/jpeg'%>
<% end %>
Let me know if you have any doubts
I remember that I used this to upload a xml file in Rails
view:
<%= form_tag({action: :upload}, multipart: true) do %>
<%= file_field_tag 'xml_file' %>
<%= submit_tag 'Submit' %>
<% end %>
controller:
def upload
file_data = params[:xml_file]
end
It is using form_tag but it would not be hard to add other info into that form also.
How to upload a image file using ajax by using prototype.js plugin, I need to display those image after it is uploaded please help to solve this problem.
Thanks
You will need something on the server, too.
I recommend using paperclip for storing the files on your server.
Once you have it set up, you should make your ajax generate one form with a "file" field. Also, remember that the form must be multipart.
<% form_for #picture, :html => { :multipart => true } do |f| %>
<%= f.file_field :file %>
<%= f.submit "Submit" %>
<% end %>
If you just need to upload one file, you probably don't need full AJAX - only plain javascript - for showing/hiding the form. Like this:
<%= link_to_function 'Show/Hide image upload') do |page|
page.visual_effect :toggle_blind, 'upload_image'
end
%>
<div id='upload_image' style='display:none'>
<% form_for #picture, :html => { :multipart => true } do |f| %>
<%= f.file_field :file %>
<%= f.submit "Submit" %>
<% end %>
</div>
Notice that for hiding/showing the div I'm using a Scriptaculous effect, not just prototype - scriptaculous gets included by default on rails anyway.
you can use remote_form_for with a file upload plugin like attachment_fu or paperclip and then render the image back on the view once it is uploaded. May be using update_page in controller.
https://github.com/JangoSteve/remotipart
Remotipart is a Ruby on Rails gem enabling AJAX file uploads with jQuery in Rails 3.0 and Rails 3.1 remote forms. This gem augments the native Rails jQuery remote form functionality enabling asynchronous file uploads with little to no modification to your application.
gem 'remotipart', '~> 1.0'
bundle install