Hope anyone can help on this!
Making use of Paperclip to upload Files to an Application, so two things that i need to cover.
1) What is the best way to link to the File for Download, I am currently linking to the public folder but it is not the best option as it shows the URL which i dont want. Thinking maybe a button_to call be not sure if this is build into Paperclip already. Options.
2) When above is done then the Browser needs to force to download not just open the file, or at least give the user the standard firefox option open or save.
Please Assist Thanks a Mil!!!
I'll answer question NÂș2.
Let's say your model is called gallery.
In your gallery controller you'll add a download method:
def download
#gallery= Gallery.find(params[:gallery_id])
send_file #gallery.gallery_picture.path,
:filename => #gallery.gallery_picture_file_name,
:type => #gallery.gallery_picture_content_type,
:disposition => 'attachment'
end
Now from your routes you'll invoke the root to this method:
match 'gallery/:id' => 'gallery#download', :as => :download
In your views:
- #galleries.each do |gallery|
= link_to 'Download Picture', download_path(gallery.id)
I'm away from home and I can not test this code, but you can visit those questions I did with the same question as yours. Let me know if it solves your problem.
Rails send_file multiple styles from Paperclip, how can I avoid code repetition?
Rails 3.0 associated model with download 'save to' image
In rails, send_file with a parameter set by user is a security hole. More information
In rails 4 you can't just write match, you have to add at the end , via: [:get, :post]. More information
Related
i am currently trying to upload a file with an ajax request on rails. My controller code is (main action):
def changePage
#welcome = WelcomeController.new
respond_to do |format|
format.html
format.js
end
end
My application is single paged, and the pages are changed via ajax requests and everything is working (but i am doint it with link_to, while in this case i use form_for - i do not know if this can be the cause). So in the view (changePage.html.index) i am rendering a partial view (_main.html.slim) which has the following code:
= form_for(:uploaded_file, :remote => true, :url => {:action => 'changePage'}, :html =>
{:multipart=>true}, :authenticity_token => true) do |f|
div class="browse"
span
| Choose file..
= f.file_field :uploaded_file
div id="file-status"
| You have not selected any files yet.
= f.submit :value => "Upload"
So when the Upload button is triggered i end up with a normal request to the server, and not an ajax request (i have changePage.js.erb file). So if someone has some idea about why this is happening it would be nice. {:
Thanks in advance!
For ajax upload you can use gem remotipart.
I am using a javascript plugin File-Uploader . It's simple and works well . I hope it would help you ^_^
You cannot just upload file with an AJAX request, it's technically impossible. So, your options are:
Use normal upload with full page request to keep it simple.
Use ugly hacks e.g. hidden iframe to achieve AJAX-like upload.
Use third-party uploader (there are plenty of them).
I suggest either option #1 or #3, depending on your requirements. #1 if you just need a simple upload, #3 if you need advanced features e.g. multiple files upload, drag & drop upload, chunked upload (for large files) etc.
hi help me in simple question:
how to released simple download:
i my public/data i have some.txt or some.pdf file ( with some text )
and i want to user click in some button and start download this file.
i do something like that
<%= link_to "Terms" ,:action => :download, :file_name => 'some.txt' %>
def download
send_file "#{RAILS_ROOT}/public/data/#{params[:file_name]}", :type=>"application/zip"
end
But what to do next?
PS if you have some tutorial or example on this subject (like downloading file), I would be very grateful
You are definitely missing a route in your routes.rb:
resources :posts do
get :download, :on => :collection
resource :comments
end
and then you can have a link like:
<%= link_to "Terms" ,:action => :download, :file_name => 'some.txt' %>
that will probably generate something like:
Terms
I think it's still not the best solution, but solves your problem.
I recommend a quick look at this part of the Rails Guides to clarify this:
http://guides.rubyonrails.org/routing.html#adding-more-restful-actions
A complete read of this guides is always good and highly recommended too.
What exactly do you mean "what next"? Apart from the fact that the application/zip mime type doesn't match your file's (which is .txt, so presumable text/plain) what you wrote is essentially correct.
Two things to keep in mind, since your file is in the public directory you don't strictly need to use send_file through the controller, you could just write this instead:
<%= link_to "Terms", '/data/some.txt' %>
And second if you're going to use send_file to server larger files you might want to look in to xsendfile instead which improves performance. This has to be supported by your webserver though (using mod_sendfile for apache for example.)
But all that said your code should work as is.
i've been using Paperclip, when deleting is done, it is just deleting the attachments, but the images saved in the file system is not being deleted.can anybody plz tell how to delete the images from the file system using paperclip itself.
the code i've been using is
has_attached_file :image_o_filename,
:styles => {
:tn => ["100x100#",:jpg ,:name => :tn],
:w => ["640x480>",:jpg, :name => :w],
:l_tn => ["200x150#",:jpg, :name => :l_tn]
},
:path => "/places/hotels/:image_pre_path/images/:basename_:style.:extension"
when i delete the photos from the view, images are getting deleted from the database but not from the file system with the above path, images remain without getting deleted. what might be the reason ??
and no errors generated in console also..
Paperclip has a very nice built in delete method, I was stuck on a very similar problem and was very well informed in this solution:
I created a method inside the controller called delete_image then from the view I could call this method.
def delete_image
#user.image.destroy
end
I had the image attached to a user instance and this worked perfect, don't forget to define your method in the routes file in your config/routes.
Good luck also you can look up more information here:
See: Rails Paperclip how to delete attachment?
I'm pretty new to Ruby on Rails.
I changed this thread, because i recognized that I was searching for my problems' solution at the wrong end.
Here's my Problem:
I got a Class ProfileProposal which I upload an Image with(Using CarrierWave).
Now I want to convert ProfileProposal to another class, called Profile.
So I pass all the Information to the NEW-Form of Profile.
Works fine with strings, but not with Images.
What I've already tried/done:
Pass the Image as GET Param to the Create Method:
<%= form_for #profile, :url => { :action => "create", :controller => "profiles", :image => #profile_proposal.image } do |f| %>
#
Which now works, so I DO have the image-url.
What's not working is the following:
#profile = Profile.new(params[:profile], :image => new_image_url)
# OR
#profile.image = new_image_url
#profile.image still has the default value given by Carrierwave.
Thanks in advance!
I'm coming from using paperclip, not carrierwave, so I'll try to keep this high level. But I have an idea for you. Maybe you can set the filename of the new attachment before it exists, then move the image to that path. With paperclip this would play out like:
#profile.image_file_name = "profile.jpg"
# creates the directory of the new path. There's probably a better way to do this:
FileUtils.mkdir_p #profile.image_file_path.gsub(/[^\/]*$/,'')
FileUtils.mv #profile_proposal.image_path #profile.image_path
You should embed a hidden field in that Profile form, referencing ProfileProposal by some ID. Then while handling the form server side, after everything is validated and ready for save, you should copy the image using some read/write methods, from ProfileProposal instance to Profile instance. I'm not sure how CarrierWave wants you to do this.
I finally fixed that problem, by using paperclip and creating a new Instance via
Profile.create(:name => #profile_proposal.name, :image => #profile_proposal.image)
This is tough one to explain so i'll try my best, and hopefully edit the question if people need more information. I am not providing exact code, but merely an example of the issue.
I am using rails 2.3.8. I am on Unix.
I have a bunch of files under a directory not Apache accessible. (i.e. /data/files/file.rpk)
I have the following in my view.
link_to "RPK File", :controller => 'mycontroller', :action=> 'myaction', :file => '/data/files/file.rpk'
I have the following in my controller.
def myaction
if FileTest.exists?(params[:file])
render :file => params[:file]
end
end
When i select the link on the page i get a download prompt for my desired file, but the name of the file is "myaction" instead of the filename.
Thoughts on how i could get it named correctly?
Sounds like a job for send_file. The x_sendfile option prevents that your workers keep busy while transferring the actual file. You can read more about that in this blogpost.
send_file path_to_file_on_filesystem, :type => "application/zip", :x_sendfile => true
You want to use send_data with the :filename option. See the API documentation.
You want to be extremely careful with this, though. Never ever trust the client/user! They will send file=../../../../etc/group or something in order to read arbitrary files on your system, so be very sure to sanitize that value before passing it to any file-reading methods.