Uploading files in Rails

2020-04-21 18:40发布

问题:

I want to make a section in my site where visitors can upload files (pdfs). How can this be done?

Also, after the pdf is uploaded, I need a link to that file. Rather than using a boring text link, I was thinking of having a thumbnail graphic of the first page of the pdf. Is there a tool out there that does this?

回答1:

For the Uploading part you might consider ActsAsAttachment or (as already stated) Paperclip.

I am not aware of any plugin for rails which is able to create/read thumbnails from PDF files automatically, but you could invoke LibExtractpr or GhostScript from your code.

Additionally you might take a look at the rGhost Gem which (as the documentation states) is a "a document creation and conversion API" which essentially should be a GhostScript Wrapper for Ruby.



回答2:

Whilst, not directly addressing the PDF thumbnail issue "The Rails Way" is doing a series on uploading/downloading files with Rails which would be useful with large files like your PDFs.

Uploading Files: basically, they have an Apache module called ModPorter which speeds up the parsing of the multipart HTTP request containing the upload.

Downloading Files: use X-SendFile to let Apache serve the download, once your Rails app has performed any user authentication required