active storage - prevent download - open file in browser when possible

微笑、不失礼 提交于 2021-01-29 02:29:06

问题


I am using rails_blob_url(o.audio_file) but it is annoying since url doesn't open file in browser but downloads the file as an attachment (this is not a default browser behaviour for audio files).

I want that browser opens the file however it wants. I do not want to force download file ...

rails_blob_url(o.audio_file, disposition: :inline)

Doesn't work.


回答1:


In addition to using rails_blob_url(o.audio_file, disposition: :inline) you also have to allow the content type.

Rails.application.config.active_storage.content_types_allowed_inline += [
  "audio/mp3",
  "audio/mp4",
  "audio/mpeg"
]

At this time the default content types are: ["image/png", "image/gif", "image/jpg", "image/jpeg", "image/tiff", "image/bmp", "image/vnd.adobe.photoshop", "image/vnd.microsoft.icon", "application/pdf"] so I had to add all the audio ones.

There's a little bit more info about it here



来源:https://stackoverflow.com/questions/56688258/active-storage-prevent-download-open-file-in-browser-when-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!