使用paperclip设置docx的内容配置

Man*_*ran 3 ruby-on-rails amazon-s3 paperclip

我试图Content-Disposition在s3中添加到我的docx文件.有点像:Content-Disposition: attachment; filename="filename.docx".我想这样做是因为IE(<9)将docx文件作为zip文件下载.经过一些谷歌搜索,我发现有一个解决方法,通过添加content-disposition内容也是如此.我尝试使用 before_post_process回电并做了

before_post_process :set_content_disposition

def set_content_disposition
  filename = self.attachment.instance.attachment_file_name
  self.attachment.instance_write(:content_disposition, "attachment; filename="+filename) 
end
Run Code Online (Sandbox Code Playgroud)

但是,它仍然以zip文件的形式下载.有没有办法正确地做到这一点.

Nud*_*oll 9

Prozac的答案(使用before_post_process编辑选项)对我不起作用.但是,无论如何,现在有一种更简单的方法.您可以将proc直接传递给has_attached_file调用的options散列中的:s3_headers键:

has_attached_file :attachment, {
  ...,
  :s3_headers => lambda { |attachment|
    # pass whatever you want in place of "attachment.name"
    { "Content-Disposition" => "attachment; filename=\"#{attachment.name}\"" }
  },
  ...
}
Run Code Online (Sandbox Code Playgroud)

  • 在最近的Paperclip中,似乎此过程在填充附件之前运行。```{“ id”:null,“ document_file_name”:null,“ document_content_type”:null,“ document_file_size”:null,“ document_updated_at”:null,“ solution_id”:null,“ user_id”:null,“ created_at”: null,“ updated_at”:null}```设置后如何获取附件详细信息,以便我可以动态设置Content-Disposition? (2认同)