【问题标题】:setting content-disposition for docx using paperclip使用回形针为 docx 设置内容配置
【发布时间】:2011-12-06 07:01:28
【问题描述】:

我正在尝试将Content-Disposition 添加到我在 s3 中的 docx 文件中。类似于:Content-Disposition: attachment; filename="filename.docx"。我想这样做是因为 IE (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

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

【问题讨论】:

    标签: ruby-on-rails amazon-s3 paperclip


    【解决方案1】:

    百忧解的回答(使用 before_post_process 编辑选项)对我不起作用。但是,无论如何,现在有一种更简单的方法。您可以将 proc 直接传递给 has_attached_file 调用的选项哈希中的 :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}\"" }
      },
      ...
    }
    

    【讨论】:

    • 谢谢,非常小的评论,总有办法避免在 ruby​​ 中转义,例如,即使你已经拥有',你仍然可以使用%(attachment; filename="#{attachment.name}")
    • 在最近的 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】:

    我终于找到了一种方法.. 有一个回形针 gem 的 before_post_process 回调。

    我们可以做这样的事情..

    has_attached_file :sample
    before_post_process :set_content_dispositon
    
    def set_content_dispositon
      self.sample.options.merge({:s3_headers => {"Content-Disposition" => "attachment; filename="+self.sample_file_name}})
    end
    

    【讨论】:

    • 这应该是.merge!
    【解决方案3】:

    我无法为您提供回形针,但 docx 文件的正确 MIME 类型/内容类型application/vnd.openxmlformats-officedocument.wordprocessingml.document

    使用它将阻止 IE 将它们下载为 zip 文件。

    以下是新办公格式的所有 MIME 类型。

    Extension   MIME Type
    .xlsx   application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
    .xltx   application/vnd.openxmlformats-officedocument.spreadsheetml.template
    .potx   application/vnd.openxmlformats-officedocument.presentationml.template
    .ppsx   application/vnd.openxmlformats-officedocument.presentationml.slideshow
    .pptx   application/vnd.openxmlformats-officedocument.presentationml.presentation
    .sldx   application/vnd.openxmlformats-officedocument.presentationml.slide
    .docx   application/vnd.openxmlformats-officedocument.wordprocessingml.document
    .dotx   application/vnd.openxmlformats-officedocument.wordprocessingml.template
    .xlam   application/vnd.ms-excel.addin.macroEnabled.12
    .xlsb   application/vnd.ms-excel.sheet.binary.macroEnabled.12
    

    【讨论】:

    • 查看我的答案。您提到的解决方案是我们从服务器提供文件时。
    • 很高兴您有一个可行的解决方案,但您仍然使用正确的 Content-Type 来处理从 S3 存储和提供的文件。
    【解决方案4】:

    百忧解,

    我认为在 Paperclip 中您必须设置 s3_header['Content-Disposition'] 散列,但鉴于 s3_header 散列未内插,我遇到了同样的问题我仍然无法弄清楚如何将文件名放在那里补丁回形针

    检查此解决方案http://groups.google.com/group/paperclip-plugin/browse_thread/thread/bff66a0672a3159b

    【讨论】:

    • 很棒的发现 prozac,但我有一个稍微不同的场景,我有一个自定义处理器,我试图实现你在这里响应的内容,但我做不到,因为如果你这样做,似乎 before_post_process 不会运行有一个定制的处理器。您知道如何与自定义处理器一起使用吗?
    猜你喜欢
    • 2017-12-02
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    • 2018-06-21
    • 2011-03-27
    • 2011-10-27
    相关资源
    最近更新 更多