Rails Rails send_file disposition 文件下载问题

bighuzi · April 07, 2017 · Last by jasl replied at April 12, 2017 · 2049 hits

ruby 代码如下。

send_file "#{Rails.root}/public/aaa.apk", filename: 'aaa.apk', disposition: 'attachment', :type=>"application/octet-stream;charset=utf-8"

api 中有提到两种方式

:disposition - specifies whether the file will be shown inline or downloaded. Valid values are 'inline' and 'attachment' (default).

我 disposition 设置为下载模式。。在 pc 端打开是可以正常下载, 当我用手机浏览器打开的时候为什么是现实的原文件。。手机中需要设置什么才能让它是直接下载呢?

没有人遇到这个问题么。。。

这个问题你要换个思路 你提到 pc 端可以正常下载,说明你用对了的,但是移动端不 OK(看你提供的文件名是要在 Android 下搞)。 通过文档可以知道,send_file 其实是会为响应头增加 Content-Disposition 段,attachment 其实就是字段的值(其实这也算是 HTTP 方向的基础知识啦)。

那么问题就要转化成,Android 的浏览器对于Content-Dispositionattachment 的处理是否有坑? 于是 Google 的关键词就有了:android browser Content-Disposition attachment 没错,只是把一堆关键字罗列一下

可以看到很多相关结果,如:

于是....

You need to Sign in before reply, if you don't have an account, please Sign up first.