Uploading/Downloading files - Ruby On Rails system - ruby-on-rails

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

You should look at Paperclip gem https://github.com/thoughtbot/paperclip
It is very easy to use and allows to upload files.

Look at Paperclip. It does a lot of the heavs lifting for you: https://github.com/thoughtbot/paperclip

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.

Related

Rails file upload: upload a folder

I work on Rails project and client asked if I can add 'upload a folder' feature to simple file upload system that we have now. Currently it attaches files to model and then displays them on a page for download. Pretty basic.
But I can't figure out how can I handle folder uploads, with every folder having it's own content. Is there any pre-made gems that can help accomplish that?
We use Paperclip at the moment, but I don't mind migrating to Carrerwave or some other gem that would
UPDATE I see that I was unclear about my needs. I need an upload system that could handle folders. Something like this.
In Dropbox I am able to upload both files and folders. How can I make my uploaders accept folders and then display them alongside regular attached files?
you can solve it by using the interpolation of paperclip where you can create or naming the folder dynamically for the same you need to do like below
specify the path into model which you wanted always
:path => ":folder/:id_:filename"
and specify the private method in same model or using globally specify in initializer
Paperclip::interpolates :folder do |attachment, style|
attachment.instance.name
end

Upload Files to a particular Folder in Ruby On Rails 4

I am using working on a particular module in Ruby on Rails, where I want to upload files to a particular folder. If it's possible, would you please share some examples which may help me achieve this functionality?
As #dharam mentioned. you can use paperclip. The working example you can find here
demo
If you want to specify folder path where the attachments should move. you need to write this in your model
has_attached_file :attachment, :path => ":rails_root/attachments/:id/:style/:basename.:extension"
After this all your attachments will be seen in attachments folder in your Application root
paperclip gem supports multiple storage options, including file storage.
Refer to FileSystem for more info.
Other gem you can look into is carrierwave

How might I be able to link jquery file upload, ajax, and jquery file download on rails?

Thanks!
I'm a complete beginner but I'd like to know even in a generic way so that my users could upload a file to another user's directory, the file gets saved on to a server and the receiving user could download it when the upload gets finished.
Since you're a complete beginner, I won't give you a lot of code; instead, I'll show you the overview of what you can do, and hopefully that will give you a much more refined approach to adopt:
Rails Uploads Using Paperclip
What makes Rails so powerful is the gem ecosystem - these are little plugins which give you functionality outside the scope of the Rails framework. You can get Gems for everything from file uploads to CSS frameworks - all for free
One of the most important & popular gems for Rails is Paperclip (and you have Carrierwave which also does the same job). These are what you use to process image uploads in Rails
Since you're a total beginner, you should really watch this Railscast about how to upload files with Rails using Paperclip:
There are also a lot of other tutorials on Paperclip:
Heroku's Paperclip Tutorial
Simple Paperclip tutorial
Another Paperclup tutorial
I would write some more updates about Paperclip & JQuery, but I think you should get your head around the upload process first

Extracting uploaded archive to S3 with CarrierWave on Heroku

I want to do something what I thought will be a simple task:
Have a form with these controls:
File upload for one file
Checkbox if this file should be extracted
Text input where I would specify which file should I link to (required only if the checkbox is checked) - index_file
After submitting form:
If the checkbox isn't checked, upload the file via CarrierWave to S3 to the specified store_dir
If the checkbox is checked, extract all files from the archive (I expect only ZIP archives; I need to keep the directory structure), upload extracted files to the specified store_dir and set the index_file in database (I don't need to save to database anything about other extracted files)
As I have found, it isn't an easy task because of Heroku limitations. These files will have a large size (hundreds of MiBs or a few GiBs), so I don't want to redownload this file from S3 if possible.
I think that using Delayed Job or Resque might work, but I'm not exactly sure how to do it and what is the best solution of my problem.
Does anyone have any idea how to solve it with using the lowest resources as possible? I can change CarrierWave to another uploader (Paperclip etc.) and my hosting provider too if it isn't possible on Heroku.
I was also thinking about using CloudFlare, would this still work without problems?
Thank you for answers.
Based on this heroku support email, it would seem that the /tmp directory is many gigs in size. You just need to clean up after yourself so Heroku as a platform is not the issue.
A couple of articles may help you solve the problem:
https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Make-Carrierwave-work-on-Heroku - which explains how to configure your app to use the /tmp directory as the cache directory for CarrierWave. Pay attention to the following line:
use Rack::Static, :urls => ['/carrierwave'], :root => 'tmp' # adding this line
This instructs rack to serve /carrierwave/xzy from the /tmp directory (useful for storing images temporarily)
Then, using the uploader.cache! method, you can deliberately cache the inbound uploaded file. Once stored, you can do checks to determine whether to call the uploader.store! method which will promote the contents to S3 (assuming you configured S3 as the store for CarrierWave.

File uploading rails 3

I have a rails 3 app that has a comment system that allows a user to make comments. What I now wish to do is extend this further by enabling the user to attach/upload files to the comment system. Fairly easy question I am about to ask. What is the best tool/plugin or gem to use so that I can achieve this? I've done some thorough research and found that alot of these plugins such as carrierwave, paperclip and a few others are used to upload photos and I am not trying to do this.
Requirements
User should be able to upload/attach files
Can upload word, powerpoint or execel docuements
Both carrierwave and paperclip can be used to upload any type of files - they have extra features if you want to upload images, but they are not restricted to handling just image files.
In the end it's a matter of preference. I found both paperclip and carrierwave to be very good at what they do.
Paperclip
CarrierWave
As for me CarrierWave more modern and nice solution. Have a try.
You can use even dragonfly - https://github.com/markevans/dragonfly/

Resources