我需要一个有效的 session 才能从我的网络应用程序下载文件。
当我在我的 iOS 设备上打开我的网络应用程序、登录并尝试从我的网站下载文件 (PDF) 时,我被踢出并改为在 Safari 中打开链接。用户必须从 Safari 重新登录才能下载页面。
如何强制文件在网络应用程序中打开并触发下载表单?
我正在使用 Carrierwave 并且在 Controller 中我有这个来触发下载:
class DocumentsController < ApplicationController
# .. code omitted
def download
if @document.name.file.exists?
send_file @document.name.path, x_sendfile: true
else
redirect_to documents_url, notice: t(:file_not_found)
end
end
end
更新:我找到了直接在浏览器中打开文件的半工作解决方案:
$(document).ready ->
if 'standalone' of window.navigator and window.navigator.standalone
# For iOS Apps
$('a').on 'click', (e) ->
e.preventDefault()
new_location = $(this).attr('href')
if new_location != undefined and new_location.substr(0, 1) != '#' and $(this).attr('data-method') == undefined
window.location = new_location
return
return
通过使用这段代码,我们强制以全屏模式(网络应用程序)运行的 iOS 用户打开应用程序内的所有 a
链接。但问题是,如果用户打开文件,将没有“后退”按钮。由于 iPhone 用户缺少物理后退按钮,他们将不得不强制重启整个应用程序才能退出显示的文件。
最佳答案
使用您发布的代码,而不是将整个页面内容写入正文,您可以将其输入到一个单独的 div 中,并确保有一个仅适用于 iOS 设备的“后退”按钮:
$(document).ready ->
if 'standalone' of window.navigator and window.navigator.standalone
# For iOS Apps
$('a').on 'click', (e) ->
e.preventDefault()
new_location = $(this).attr('href')
if new_location != undefined and new_location.substr(0, 1) != '#' and $(this).attr('data-method') == undefined
window.location = new_location
return
return
关于iOS “Add to Home Screen” - 文件下载将用户踢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32332917/