Uploading/Downloading files - Ruby On Rails system
Solution 1
You should look at Paperclip gem https://github.com/thoughtbot/paperclip
It is very easy to use and allows to upload files.
Solution 2
Look at Paperclip. It does a lot of the heavs lifting for you: https://github.com/thoughtbot/paperclip
Solution 3
As they said look at paperclip. I just did a app that allows the users to upload and delete files. To get started with paperclip use http://railscasts.com/episodes/134-paperclip
To download files after uploading them with paperclip. I did the following in the controller
def download
upload = Upload.find(params[:id])
send_file upload.uploaded.path,
:filename => upload.uploaded_file_name,
:type => upload.uploaded_content_type,
:disposition => 'attachment'
flash[:notice] = "Your file has been downloaded"
end
My sample file upload app should be of help https://github.com/skillachie/File-Upload-App Need to fix a few things , but the ability to upload and download files is completely functional.
Admin
Updated on June 05, 2022Comments
-
Admin almost 2 years
I'm attempting to create a simple file hosting system using Ruby On Rails - I have a large ammount of the system setup (including the registration of new files, and stuff) however I've realised there is a bit of a problem - I'm unsure how to actually get it so that users can upload and download files. I assume I'd need some kind of file_link attribute for my file object, but how would people upload and download files to/from the server?
Also (this may be a slightly different topic) - but how would I get the file information such as file size and name (as I need them for the upload)?
Sorry for all my questions - I've don't really deal with file handling a lot so am new to the area.
Thanks In Advance,
Regards,
Joe
-
Admin about 13 yearsOk - Thanks :D Just got everything setup with Paperclip. The issue is that with image files the Download link just views the file (and I can imagine that with PDF files they would just open with Adobe Reader) - is there any way I can MAKE the link be for a download?
-
Admin about 13 yearsOk - Thanks :D Just got everything setup with Paperclip. The issue is that with image files the Download link just views the file (and I can imagine that with PDF files they would just open with Adobe Reader) - is there any way I can MAKE the link be for a download?
-
Rafal about 13 yearsYes, you have to use
send_file '/home/railsway/downloads/huge.zip', :type=>"application/zip"
in your action to send the file instead of displaying it -
Rafal about 13 yearsObviously change the link to be your file, and the right type. So you need to call an action with the id of your uploaded file and then send it using send_file
-
Rafal about 13 yearsI recommend you read this article to get a better understanding. therailsway.com/2009/2/22/file-downloads-done-right
-
Admin about 13 yearsI changed my link to:
<%= link_to 'Download', :action => :download, :path =>@file_record.data.url, :type => @file_record.data_content_type %>
and used the def from "therailsway" in my controller. The problem is that it takes me to this URL:http://127.0.0.1:3000/file_records/download?path=../download/8/Joe.png%3F1297359980&type=image/png
and brings this error:Couldn't find FileRecord with ID=download
. I think this is due to my routes which make /file_record/id go the the files ID. Please help.. -
Rafal about 13 yearsWhy dont you pass only the id
<%= link_to 'Download', :action => :download, :id =>@file_record.id %>
And then you get the object in the controller with find, and send it. -
Rafal about 13 years
fileRecord = FileRecord.find(params[:id])
The rest is the same as you have -
Admin about 13 yearsNow I get:
Cannot read file ../download/8/Joe.png?1297359980
. I assume this is because I've set my paperclip path to ../download/:id/whateverelse (as the download link is in /files in the URL and I want /download to just come from the URL). I have tried changing my paperclip URL to/download/:id/:basename.:extension
but it just has the same error (but without the ../). I'm unsure as to why this is and can't seem to fix it.. (Please note, my path is set to":rails_root/public/download/:id/:basename.:extension"
) -
Rafal about 13 yearsI suppose you are using the database to store your file informations, so when you call
send_file
you can provide it with the absolute path on your server such asRAILS_ROOT + "/public/download/" + @file_record.file_name
file_name, must be the name on disk. Try it with a static absolute path, and then modify accordingly to be dynamic. -
Admin about 13 yearsOk - Thanks :D I've now got it downloading the files correctly, however there is a HUGE problem. Whenever the download button is clicked, Google Chrome (The browser I'm using for testing) downloads a .crdownload file and then takes about a minute to convert it into the proper file downloaded - do you know why this is?
-
Rafal about 13 yearsare you sure that the
:type=>"application/SOME_TYPE"
parameter is correct for your type of file it should be the type stored file_content_type ... i noticed that for Images, it sets it to octet-stream , maybe try png or jpg depending on your file (:type => 'image/jpeg') -
Admin about 13 yearsI've just tried this - however it still has this weird issue. You have to wait about a minute or two before it actually turns into the correct file.. (Until then it just stays as a .crdownload file). Please help, this is a huge issue in my system (I need downloads to be as fast as possible).
-
Rafal about 13 yearsit seems like a "Feature" of chrome that you can remove. Look at this thread. google.com/support/forum/p/Chrome/… Did you try with another browser ? Firefox for example ?
-
Admin about 13 yearsFirefox downloads the file - but when I try to view it, it doesn't open as when trying to open it the file is "unsupported or corrupt". The file is exactly 1 byte (which is incorrect as the actual file is larger) - this is the same thing that happened with Chrome (apart that chrome fixed the file in the end).
-
Rafal about 13 yearsAre you using Rails 3 ?? There is an answer to this question in another thread. stackoverflow.com/questions/3724853/… The problem is that you need to set
send_file ...., :x_sendfile=>false
Try that and read the answer, it might give you a path to your solution -
Admin about 13 yearsI actually had :x_sendfile => true in my code, changing it to false fixes it. I was told to set it to false to enable downloads with multithreads (and I was told that downloads would be slow otherwise).
-
Ayman Salah over 8 yearsWill this work if I have my website on heroku? How much data will I be able to upload?