Get server file path with Paperclip - ruby-on-rails

I'm using Rails with Paperclip to make a small file upload app. I would like to be able to return the file path on the server of the uploaded file once its done but I can't seem to work out how to get the path? Paperclip only seems to record the name of the file itself.
Does anybody now how to do this?

Assuming you had an attachment called avatar on an instance of a user, you can use user.avatar.path to get the full path of the file on the filesystem, and you can use user.avatar.url to give the path which you could use in image tags and whatnot.
Is that what you're meaning?

I came cross the same problem, so I made a link to it's url in show.html.erb. It works.
<p>
<b>Pdf:</b><%= link_to "PDF" , #product.pdf.url %>
</p>

Related

Rails: Refile for documents to download

Using the refile gem, I have uploaded documents (.pdf, .docx, .pptx, etc.). Uploading is fine. When I use attachemnt_url, it produces something like /attachments/...234jksdf2.../document. When I click the link_to, it downloads the document without an extension.
What's happening to make it operate this way? How can I restore my file type sanity?
I was trying to address the exact same issue, this is one approach I tried:
Refile allows you to save additional metadata such as the content_type: https://github.com/refile/refile#additional-metadata. The resulting file content type will be saved as something like "image/png" or "application/pdf".
We can then apply something like
link_to "Download file", attachment_url(#document, :file, format: #document.file_extension)
Whereby
in document.rb
def file_extension
file_content_type.split("/").last.to_sym
end
The only issue is that this doesn't automatically downloads the file, but rather opens it in a new page where you can then download the file. Still looking for better alternatives!
This is what ended up being the correct solution for me.
link_to "Download file", attachment_url(#document, :file, format: #document.file_extension)
def file_extension
require 'rack/mime'
Rack::Mime::MIME_TYPES.invert[document_content_type].split('.').last
end

Rails Paperclip uploads numbers after url

Whenever I upload a file through paperclip the end of the URL gets kind of messed up, for instance, in stead of the expected URL
http://localhost:3000/assets/1/file.pdf
I get
http://localhost:3000/assets/1/file.pdf?1415287826
The url and path in my model are:
has_attached_file :file, url: "/assets/:id/:basename.pdf",
path: ":rails_root/public/assets/:id/:basename.pdf"
The actual file is not stored like this, this is only how the url looks using
<%= link_to "Open PDF", upload.file.url %>
It seems like an easy to fix problem but I just can't find the solution....
It's timestamp for your file. It holds the time when your file was uploaded to the server: in this case Time.at(1415287826) => 2014-11-06 15:30:26 +0000
When you will download that file, it will be cached by the browser (images are cached by default, pdfs can be cached now if browser supports pdf rendering). With that timestamp if you upload a file with exactly the same name (so the same url will be generated), browser will not highlight that link as visited. As a result, if it's an image with the same name, its cached version will not be rendered, because browser will considers such link as not visited therefore not cached.

Display PDF in iframe in RoR

I would like to display a pdf in an iframe in one of my Rails projects. My current code is <iframe src="tmp/data.pdf"></iframe>, but when I go to this page, I get the error: "No route matches [GET] /Users//development//tmp/data.pdf".
data.pdf is in this location, so I think I'm doing something wrong with my routes file, and maybe I have to route the file to a path appropriately. I've tried playing around with a few things, but haven't had much luck so far. Can anyone provide any help?
Rails only exposes the contents of the "public" subfolder to the webbrowser so no code/configuration can be downloaded by any user...
Try putting the file there and it should work.
UPDATE 1
Also you need to note, that the "public" part does not have to be included in the URL. So in your case the url would be just "/data.pdf".

How to download image from url and display in view

I am trying to download an image and displaying it in a view in rails.
The reason why I want to download it is because the url contains some api-keys which I am not very fond of giving away.
The solution I have tried thus far is the following:
#Model.rb file
def getUrlMethod
someUrlToAPNGfile = "whatever.png"
file = Tempfile.new(['imageprependname', '.png'], :encoding => "ascii-8bit")
file.write(open(data).read)
return "#{Rails.application.config.action_mailer.default_url_options[:host]}#{file.path}"
end
#This seems to be downloading the image just fine. However the url that is returned does not point to a legal place
Under development I get this URL for the picture: localhost:3000/var/folders/18/94qgts592sq_yq45fnthpzxh0000gn/T/imageprependname20130827-97433-10esqxh.png
That image link does not point anywhere useful.
My theories to what might be wrong is:
The tempfile is deleted before the user can request it
The url points to the wrong place
The url is not a legal route in the routes file
A am currently not aware of any way to fix either of these. Any help?
By the way: I do not need to store the picture after I have displayed it, as it will be changing constantly from the source.
I can think of two options:
First, embed the image directly in the HTML documents, see
http://www.techerator.com/2011/12/how-to-embed-images-directly-into-your-html/
http://webcodertools.com/imagetobase64converter
Second, in the HTML documents, write the image tag as usual:
<img src="/remote_images/show/whatever.png" alt="whatever" />
Then you create a RemoteImages controller to process the requests for images. In the action show, the images will be downloaded and returned with send_data.
You don't have to manage temporary files with both of these options.
You can save the file anywhere in the public folder of the rails application. The right path would be something like this #{Rails.root}/public/myimages/<image_name>.png and then you can refer to it with a URL like this http://localhost:3000/myimages/<image_name>.png. Hope this will help.

file_field_tag : what was the original file name?

My site allows users to upload csv files for processing. It all works
fine, but on the response I'd like to report something like "Your file
abc.csv processed OK".
Unfortunately I cannot seem to find the actual original file name in
the params, even though Firebug tells me it's part of the post.
Any tips?
Thanks....
Try using debug on the results of your form.
http://guides.rubyonrails.org/debugging_rails_applications.html#debug
As Jarrod mentions in the comments above. Use params[:file].original_filename
Funny thing is, my form has two file upload tags (file1 and file2). One comes in as a ActionController::UploadedTempfile and the other ActionController::UploadedStringIO.
This may be a rails bug but it doesn't matter to me as both have the original_filename method.

Resources