Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用回形针设置docx的内容配置_Ruby On Rails_Amazon S3_Paperclip - Fatal编程技术网

Ruby on rails 使用回形针设置docx的内容配置

Ruby on rails 使用回形针设置docx的内容配置,ruby-on-rails,amazon-s3,paperclip,Ruby On Rails,Amazon S3,Paperclip,我正在尝试将内容配置添加到s3中的我的docx文件中。内容处理:附件;filename=“filename.docx”。我之所以想这样做,是因为IE(

我正在尝试将
内容配置
添加到s3中的我的docx文件中。内容处理:附件;filename=“filename.docx”。我之所以想这样做,是因为IE(<9)将docx文件作为zip文件下载。在谷歌搜索之后,我发现有一个解决方法,在内容中添加
内容配置。我试着使用
before\u post\u过程
callback并成功了

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文件下载。有没有正确的方法可以做到这一点。

我无法帮助您使用回形针,但是
docx
文件的正确MIME类型/内容类型是
application/vnd.openxmlformats officedocument.wordprocessingml.document

使用该选项将停止IE将其作为zip文件下载

以下是新office格式的所有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
百忧解

我认为在回形针中,你必须设置s3_头['Content-Disposition']散列,但鉴于s3_头散列不是插值的,我仍然不知道如何在没有补丁回形针的情况下将文件名放在那里


检查这个解决方案我终于找到了一个方法。。有一个使用回形针gem的前后过程回调

我们可以这样做

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
百忧解的答案(使用前-后-过程编辑选项)对我不起作用。无论如何,现在有一个更简单的方法。您可以将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}\"" }
  },
  ...
}

看看我的答案。。您提到的解决方案是当我们从服务器提供文件时。我很高兴您有一个有效的解决方案,但您仍然使用正确的
内容类型来存储和提供S3中的文件。很棒的find prozac,但我有一个稍微不同的方案,我有一个自定义处理器,我试图实现您在这里的响应,但我无法实现,因为如果您有一个自定义处理器,那么before\u post\u进程似乎无法运行。您知道如何与自定义处理器一起使用吗?谢谢,非常简短的评论,在ruby中总是有避免转义的方法,例如,在这里,即使您已经有了
,您仍然可以使用
%(attachment;filename=“#{attachment.name}”)
在最近的回形针中,此过程似乎在填充附件之前运行
{“id”:null,“document\u file\u name”:null,“document\u content\u type”:null,“document\u file\u size”:null,“document\u updated\u at”:null,“solution\u id”:null,“user\u id”:null,“created\u at”:null,“updated\u at”:null}
有没有办法在设置完附件后获得详细信息,以便动态设置内容配置?这应该是
合并