Download file using gem Paperclip - ruby-on-rails

I want to understand how to download file using Paperclip. I upload file to local storage.
It's Model:
class AFile < ActiveRecord::Base
has_attached_file :attach,
:url => "public/attach/:basename.:extension",
:path => ":rails_root/public/attach/:basename.:extension"
validates_attachment_content_type :attach, content_type: "text/plain"
end
It's View show.html.erb :
<p>
<strong>AFile:</strong>
<%= #afile.name_file %>
</p>
<%= link_to 'Download', #afile.attach.url(:original, false) %> |
<%= link_to 'Edit', edit_afile_path(#afile) %> |
<%= link_to 'Back', afiles_path %>
I did like this:
File download using Paperclip
but it did not help.
But when i click on the Download, then an error:
No route matches [GET] "/public/attach/text.txt"
How to solve this problem? Why file cannot be downloaded by clicking "Download"?

Rails places the /public directory in the servers web root. So a file with the file system path /public/foo.txt will be accessible at http://localhost:3000/foo.txt - not http://localhost:3000/public/foo.txt.
So you need to change url option for the attached file:
class AFile < ActiveRecord::Base
has_attached_file :attach,
:url => "/attach/:basename.:extension",
:path => ":rails_root/public/attach/:basename.:extension"
validates_attachment_content_type :attach, content_type: "text/plain"
end

My solution to download file is something like :
<%= link_to 'Download', #afile.attach.url(:original),
download: #afile.attach.url(:original)%>

Related

Paperclip image_tag does not show image with S3 and Rails

I am using Paperclip for S3 image uploading in my Rails (4.2) app. Everything works fine, except of the fact that the image_tag does not show the image but the title of the image. What am I missing?
recipe.rb
class Recipe < ActiveRecord::Base
has_attached_file :image, styles: {
thumb: '100x100>',
square: '200x200#',
medium: '300x300>'
}
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
validates_with AttachmentSizeValidator, attributes: :image, less_than: 1.megabytes
end
Recipe show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= #recipe.name %>
</p>
<div class="col-xs-12" style="height:400px">
<%= image_tag #recipe.image.url(:original) %>
</div>
<%= link_to 'Edit', edit_recipe_path(#recipe) %> |
<%= link_to 'Back', recipes_path %>
UPDATE: Solution
Two things are needed to make it work:
Add s3_host_name: "s3-#{ENV['AWS_REGION']}.amazonaws.com" to config. This is a workaround as suggested here.
Add :s3_protocol => :https to config as aws-sdk-2 needs this explicitly (as mentioned in the comments).
In aws-adk-2 you need to specify s3 protocol explicitly in your configuration :s3_protocol => :https.
latest versions of paperclip depend on aws-sdk-2. so you have to specify the protocol.
In this way this way this will save your image URL with https.
This answe will help you see the link. you have the same problem.

Rails 4 how to upload the file and the file content into a database

I have a demand,used rails 4 to upload the file,and i need save the file content into a database,file does not need to save.Now have no idea.I tried many way but failed to solve.Hope to get your help,thx.
You can use gem like paperclip(https://github.com/thoughtbot/paperclippaperclip) or carrierwave as a sample model it will be like this
class User < ActiveRecord::Base
has_attached_file :avatar,
:storage => :database,
:styles => { :thumb => "75x75>", :small => "150x150>" },
:url => '/:class/:id/:attachment?style=:style'
end
in storage you can specify the kind of saving you want
You can create a multipart form object. Please check the example below,
HTML View
<% form_for(:uploaded_file, #feed, :html=> {:multipart=>true}) do |f| %>
<p>
<%= f.label :uploaded_file, 'Upload your file.' %><br />
<%= f.file_field :uploaded_file %>
</p>
<p><%= f.submit 'upload' %></p>
<% end %>
Controller Action
file_data = params[:uploaded_file]
if file_data.respond_to?(:read)
xml_contents = file_data.read
elsif file_data.respond_to?(:path)
xml_contents = File.read(file_data.path)
else
logger.error "Bad file_data: #{file_data.class.name}: # {file_data.inspect}"
end

Show and download PDF Ruby on Rails

I am making a website where users can upload images and PDF files.
I am working with Ruby on Rails and paperclip. I have a site called "show" where the user can see his post with "Name, Description and the Picture". But now I want to make that everybody can upload images OR PDF files and that there is a download link for each file next to the picture. I am using the sqlite database.
And on the place where normally the picture is I want to have a PDF logo where they can click on to see the PDF file.
Can someone help me in this situation?
My model:
class Picture < ActiveRecord::Base
belongs_to :bill
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }
validates_attachment :image, :content_type => {:content_type => %w(image/jpeg image/jpg image/png application/pdf application/msword application/vnd.openxmlformats-officedocument.wordprocessingml.document)}
end
How I show the picture at the moment:
<%= image_tag #pictures[0].image.url(), class: "img-responsive" if #pictures.length > 0 %>
Thanks in advance.
Just add a special treatment for PDF. Something like:
<% if #picture.image_content_type=="application/pdf" %>
<%= link_to (image_tag "pdflogo.png"), #picture.image.url() %>
<% else %>
<%= image_tag #picture.image.url(), class: "img-responsive" %>
<% end %>
assuming that the image is held in variable #picture and pdflogo.png contains the pdf logo. (The use of #pictures[0] and if #pictures.length > 0 directly in the code for showing one image is not very DRY; this should be separated from the code for showing one picture.)

error uploading images using paperclip (local server)

I am getting an error when trying to upload images to my (listings) using the paperclip gem. The error that the browser outputs is: 1 error prohibited this listing from being saved: Image has contents that are not what they are reported to be **As a note, image magic has been successfully installed on my computer and there are no issues there
my listing.rb file
class Listing < ActiveRecord::Base
has_attached_file :image, :styles => { :medium => "200x", :thumb => "100x100>" }, :default_url => "default.jpg"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
my gemfile
gem "paperclip", "~> 4.3"
my listings_controller
def listing_params
params.require(:listing).permit(:name, :description, :price, :image)
end
end
and finally my form
<%= form_for #listing, :html => { :multipart => true } do |f| %>
...
...
<div class="form-group">
<%= f.file_field :image, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
It sounds like you need to include file on your system.
If you're using Windows, you need to download file from this URL, install it on your hard drive and then add it to your PATH environment var:
Click "Start"
On "Computer", right-click and select "Properties"
In Properties, select "Advanced System Settings"
Click the "Environment Variables" button
Locate the "PATH" var - at the end, add the path to your newly installed file.exe (typically C:\Program Files (x86)\GnuWin32\bin)
Restart any CMD shells you have open & see if it works
You're probably trying to attach a file that is not properly recognised as an image, or one that has an image extension and has different content (like a PDF, for example).
Some workarounds are discussed here: https://github.com/thoughtbot/paperclip/issues/1924
It might help to check the log file as well - it should tell you what Paperclip thinks the type of the attachment is.

How to Upload a multipage PDF and convert it to JPEG with Paperclip?

Does anyone know how to upload a multi-page pdf with Paperclip and convert each page into a Jpeg?
So far, every time I upload a PDF, it only allows me to see the first page of the PDF as a JPEG. But I would like to be able to upload and convert every page from the PDF into a JPEG.
Is there any gem or plug-in that can help me upload a 10-pg PDF and Convert/Store in the Database as 10 JPEG files?
I have looked at docsplit-images gem, but I am not sure if that is the solution best solution or how it works.
Post.rb
class Post < ActiveRecord::Base
belongs_to :Blogs
attr_accessible :content, :title, :pdf
has_attached_file :pdf,
:url => "/assets/products/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/products/:id/:style/:basename.:extension"
validates_attachment_content_type :pdf,
:content_type => [ 'application/pdf' ],
:message => "only pdf files are allowed"
end
_form.html.erb
<%= form_for ([#post]), :html => { :multipart => true } do |f| %>
<%= f.file_field :pdf %>
<% end %>
show.html.erb
<%= image_tag #post.pdf.url(:original) %>
Using an image tag for this makes no sense. Change your image_tag to a regular link and you'll be able to download and view all the pages.
<p>
<%= link_to 'My PDF', #post.pdf.url %>
</p>

Resources