【问题标题】:Upload docx file downloaded from google drive using paperclip使用回形针上传从谷歌驱动器下载的 docx 文件
【发布时间】:2019-02-21 02:26:39
【问题描述】:

我想使用 gem 回形针 上传从谷歌驱动器下载的 docx 文件,但我收到 无效的内容类型错误。下面是我的代码

has_attached_file :initial_document
validates_attachment :initial_document, content_type: {content_type: %w(image/jpeg image/jpg image/png application/pdf application/msword application/vnd.openxmlformats-officedocument.wordprocessingml.document)}

我可以上传从 ms word 创建的文件,但不能从谷歌驱动器上传。 调试的时候发现.docx文件的内容类型是application/zip 我用的是Rails 5.0和paperclip 5.1

【问题讨论】:

  • 你确定是application/zip吗?如果是这样,只需将其添加到 content_type
  • 是的,它的 application.zip ,我允许 application.zip 但下载时我收到了相同的文件,没有任何扩展名

标签: ruby-on-rails ruby paperclip


【解决方案1】:

问题在下面一行,

validates_attachment :initial_document, content_type: {content_type:

这里,content_type 是重复的,这是不正确的语法。

将上面替换为

validates_attachment :initial_document, 
  content_type: %w(
    application/vnd.openxmlformats-officedocument.wordprocessingml.document
    application/pdf
    application/msword
    application/word
    application/x-msword
    application/x-word
    application/zip
    text/plain
    image/jpeg 
    image/jpg 
    image/png 
  )

这里 application/zip 对 .docx 文件很重要

我已经对上述更改进行了测试,并且成功创建了文档对象,如下所示

    #<Document:0x0d0bd8f0> {
                                :id => 1,
           :document_list_file_name => "sample-document.docx",
        :document_list_content_type => "application/zip",
           :document_list_file_size => 12711,
          :document_list_updated_at => Wed, 19 Sep 2018 11:56:02 UTC +00:00
    }

希望,这将解决您的问题。

【讨论】:

  • 我的语法也是正确的,当我添加应用程序/zip时,它可以工作,但下载后,它给出的文件没有任何扩展名
猜你喜欢
  • 2018-08-06
  • 1970-01-01
  • 1970-01-01
  • 2016-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多