【发布时间】:2015-09-01 13:27:34
【问题描述】:
我需要一个有效的会话才能从我的网络应用下载文件。
当我在我的 iOS 设备上打开我的网络应用程序、登录并尝试从我的网站下载文件 (PDF) 时,我被踢出并在 Safari 中打开了链接。用户必须从 Safari 再次登录才能下载该页面。
如何强制文件在网络应用程序中打开并在那里触发下载表单?
我正在使用 Carrierwave,并且在控制器中我有这个来触发下载:
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
通过使用这个 sn-p 代码,我们强制运行在全屏模式(网络应用)的 iOS 用户打开应用内的所有 a 链接。但问题是,如果用户打开文件,将没有“后退”按钮。由于 iPhone 用户缺少物理后退按钮,他们将不得不强制重启整个应用程序才能退出显示的文件。
【问题讨论】:
标签: ios ruby-on-rails download carrierwave iphone-standalone-web-app