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
Related
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.
I'm developing a Ruby on Rails application that requires file uploading/downloading. For the upload part i used the gem carrierwave since it's very easy to use and flexible. The problem is: once i uploaded the file, i need to know a few things: i.e. if it's a pdf instead of downloading the file i show it inline,and the same goes for an image. How do i get the file extension and how can i do it to send the file to a user?? Any feedback is appreciated Thanks!!
Determine file extension (I suppose a name for mounted uploader is 'file'):
file = my_model.file.url
extension = my_model.file.file.extension.downcase
Then prepare mime and disposition vars:
disposition = 'attachment'
mime = MIME::Types.type_for(file).first.content_type
if %w{jpg png jpg gif bmp}.include?(extension) or extension == "pdf"
disposition = 'inline'
end
(add other extensions if you want).
And then send the file:
send_file file, :type => mime, :disposition => disposition
Once you have uploaded a file, the name is stored in the database. This also includes the extension. Assuming you have a User model with an uploader mounted as asset, then you can get it as:
user.asset.file.extension
As for sending it to the user, if you call user.asset_url, it will give you the URL where the file is uploaded. The user can use that link to get the file. Or am I misunderstanding what you mean by "send the file to a user"?
I'm building a website using Rails and I have a Model with some text type value store in MySQL database, I need to provide a download link for my users to download a "*.txt" file which contains those texts.
what I've tried is to use render :text => my_text but it's kind of ugly and the browser can't start a download .
Not I'm trying to use CarrierWave and mount a output_file to my model , I want to build a method to generate a file from its text value. Any suggestion would be greatly appreciated. Thanks!
send_data 'text to send', :filename => 'some.txt'
Documentation Here
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>
I have a paperclip attachment in one model, but I`m not saving the file in /public, but /assets. And when the user what to open the file I use the send_data() function, which makes the user to download the file.
My question is how can I show the file in other way (not nessecery to download)? So if the file is a image, I will see it directly in the browser without download.
Thanks!
Try:
send_data foo, :disposition => 'inline'
This would tell the browser to just render the content, instead of prompting the user to save it.
… from http://apidock.com/rails/ActionController/Streaming/send_data