【问题标题】:Rails: Refile for documents to downloadRails:重新归档以下载文件
【发布时间】:2016-04-27 18:27:08
【问题描述】:

使用refile gem,我上传了文档(.pdf、.docx、.pptx 等)。上传就好了。当我使用attachemnt_url 时,它会产生类似/attachments/...234jksdf2.../document 的东西。当我单击link_to 时,它会下载没有扩展名的文档。

发生了什么让它以这种方式运行?如何恢复我的文件类型健全性?

【问题讨论】:

    标签: ruby-on-rails refile


    【解决方案1】:

    我试图解决完全相同的问题,这是我尝试过的一种方法:

    Refile 允许您保存其他元数据,例如 content_type: https://github.com/refile/refile#additional-metadata。生成的文件内容类型将保存为 "image/png""application/pdf" 之类的内容。

    然后我们可以应用类似的东西

    link_to "Download file", attachment_url(@document, :file, format: @document.file_extension)
    

    在哪里

    in document.rb
    
    def file_extension
        file_content_type.split("/").last.to_sym
    end
    

    唯一的问题是这不会自动下载文件,而是在新页面中打开它,然后您可以在其中下载文件。仍在寻找更好的替代品!

    【讨论】:

    • 我明天尽快试试。如果您找到解决方案,请更新!
    • 这更接近了,但我从attachment_url 中得到了一个非常奇怪的网址。
    • 网址是什么,返回的究竟是什么?
    • 看起来像这样的东西:http://host.com/attachments/a8ea1c0e1212b9fe11c230ab3d0ce9110e689706/store/application/vnd.ms-excel/0e76b0554c33275d7f48030226a28f9a2d56bee2e930c4cf5af8a032497f/file-name.xls,但我得到(看起来像 nginx)not found 错误。
    • 这对于图像来说没问题,但如果它是启用宏的 excel 文件,我不会得到 .xls,我会得到 vnd.ms-excel,所以这不起作用。
    【解决方案2】:

    这对我来说是正确的解决方案。

    link_to "Download file", attachment_url(@document, :file, format: @document.file_extension)

    def file_extension 
        require 'rack/mime'   
        Rack::Mime::MIME_TYPES.invert[document_content_type].split('.').last 
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-20
      • 2019-07-27
      • 1970-01-01
      • 1970-01-01
      • 2016-12-21
      • 1970-01-01
      • 2016-01-16
      相关资源
      最近更新 更多