Looking to upload files to local file storage on server. Recommended best to handle multiple?
I am looking to upload multiple files to local file stoarage on a server. They could be images, excell files, pdf's, .dwg etc.
Do I need to somehow declare what type of file is being uploaded or can I just sort of upload anything and store a link to it in the DB?
What would you recommend to use to do this?
I am new to rails and am not too familiar with everything yet.
By default it's going to accept any file types no matter what you use for file uploads. All those files will get their own database records if you have it so each upload is sent individually. That's what we covered in this episode: https://gorails.com/episodes/multiple-file-uploads-with-shrine
Give that a watch and see if that answers most of your questions. 👍
Great. Thanks. I saw those videos but wasn't sure if it would apply since I wouldn't be using Amazon or something like that. I'll check this one out.
Yup, you just don't need to do the presigning but the Javascript library will still help you control multiple file uploads on the frontend.
EDIT: So I think I just figured it out, but how to set the href link so it only shows the filename and not the whole file_data JSON string?
So I have been starting simple since I'm new to this. I have this working by uploading one file at a time (yay!) but I have a question on how to make an href link to the file.
I have a projects controller which has a bunch of data related to a given project. What I am looking to do is upload the file (only working on a single file at first, then I'll move on to multiple) and display a hyper link to the file on the show.html.erb page.
Looking at this:
<% if @post.file.present? %>
<%= file_tag post.file_url %>
<% end %>
How would I work that into my @project as an href link? I am getting an error that post is not definied. I tried @project but that did not work either. I have named my upload file "file" instead of image.
That would be beacuse in your file_tag
you're not referencing your @post
instance variable since you only have a local post
variable there.
For links you will want to do a regular link_to:
<%= link_to "View File", @post.file.url %>
Thank you for you help. I'm almost there! How do I have the link_to display only the name of the file and not the whole URL of the file?
I got it! I was trying to display to file.filename instead of file.original_filename.
Thanks for your help. On to multiples now.
Okay great, and yeah each file upload gem is slightly different than the others so it can change the code you write just slightly which was the reason I asked.
So I ended up switching gears and going with the amazon S3 model. I have everything working properly so that is the good thing and it is awesome!
BUT
I have a one small issues that are puzzling me.
1.) I have a projects/show page. I am uploading multiple files for a given project off of this page. Once the file upload process is done I do not seem to see the new links to the files created until I refresh the page. I tried messing around with the uploads.js file to add in an li tage instead of an img. It does not seem to be working. I have no errros in the console either, everything looks clean. Here was my code:
var $file = $("<li/>", {src: response.image_url});
var $div = $("<div/>").append($file);
$("#files").append($div);
What I am trying to output on the page for each link is:
<li><i class="fa fa-file"></i><%= link_to (upload.file.original_filename), upload.file.url %></li>
Thanks!
EDIT: So I guess looking at this I am not really doing anything currently with the URL from the file upload. I am just using activerecord to get a list of the uploads for that project so it make sense to me now that they are not automatically showing up. So I guess I am looking how to work that in to do it as the uploads finish (just display a link to the files), or the other option would be to trigger a page refresh when all files are done uploading and leave the page code the way I have it now.