开发者

Paperclip - how do you upload PDFs?

I've got a model that uploads both images and pdfs to Amazon S3 - the images work, the pdfs don't.

This is what my model looks like:

validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png', 'image/gif']
has_attached_file :pdf,
    :storage 开发者_JAVA百科=> :s3,
    :s3_credentials => "#{::Rails.root.to_s}/config/s3.yml",
    :path => "/userpdfs/:id/:basename.:extension"
has_attached_file :photo,
    :styles => {:medium => "200x300>", :thumb => "100x150>" },
    :storage => :s3,
    :s3_credentials => "#{::Rails.root.to_s}/config/s3.yml",
    :path => "/userphotos/:style/:id/:basename.:extension"

Amazon is showing userphotos but no userpdfs. Both types have four fields in my database:

pdf_name
pdf_type # could also be a word doc
pdf_size
pdf_updated_at
  1. Why aren't the resumes uploading (the images do!)
  2. How can I validate the :pdf field to only allow .pdf, .doc, .docx, .txt?

Edit

Uploading any image to the :pdf field works! Something about pdfs themselves seem to mess up the system. My server says it has saved the files to S3 but they don't appear in the directory.

PDFs now work only if they have no spaces in the title. With spaces, I get the following error: Errno::EPIPE: Broken Pipe.


Try this

>> Attachment.last.file_path
=> "https://s3.amazonaws.com/content.dynamixccw.com/attachments/4/Financial goal Report.pdf"
>> url = _
=> "https://s3.amazonaws.com/content.dynamixccw.com/attachments/4/Financial goal Report.pdf"
>> url.gsub(' ', '%20')
=> "https://s3.amazonaws.com/content.dynamixccw.com/attachments/4/Financial%20goal%20Report.pdf"

To validate, throw this in

validates_attachment_content_type :pdf, :content_type => ['application/pdf', 'application/msword', 'text/plain'], :if => :pdf_attached?

def pdf_attached?
  self.pdf.file?
end


Add this 'application/pdf' to your :content_type, so it becomes :content_type => ['image/jpeg', 'image/png', 'image/gif','application/pdf']. That should fix it and if you want it opened within the application, then also add 'application/octet-stream' and for MSword/spreadsheet, add 'application/msword'.


For uploading a pdf document you can just have to do following:

In your model just type

validates_attachment_content_type :uploaded_file, :content_type =>['application/pdf']

This is working for me, i hope this will help you, All The Best.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜