如何在iPad上的Safari中预览文件?

时间:2013-06-11 09:31:39

标签: javascript ipad angularjs safari

我有一个返回文件的Web服务,我已将web服务url放入标记中,因此它变为: <a href="webserviceurl/someurl"/>,当我点击链接并尝试在iPad上的safari中打开它时,它只是给了我“safari无法下载此文件”。当我在桌面上打开此链接并查看响应文档时,该文件的标题如下所示:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Disposition: attachment; filename="test.doc"
Content-Type: application/x-msdownload
Transfer-Encoding: chunked
Date: Mon, 10 Jun 2013 02:26:50 GMT

我猜是“内容处理”会让这种情况发生,因为我尝试了一个直接链接到<a href="/somefile.doc"/>文件的网址,它会在iPad上完美地预览文件。

我无法更改网络服务,我只能修改javascript代码,而我们的应用程序是基于angularJS构建的,有什么办法可以在iPad上预览文件吗?

1 个答案:

答案 0 :(得分:0)

据我所知,ios safari不支持文件下载,当你在内容配置中有附件时,浏览器会被指示这样做。

因此,解决此问题的唯一方法是使用您可以控制的代理服务器,您的javascript应用程序与您的代理服务器通信,您的代理服务器获取您的应用程序的文件,然后将其发送到您的应用程序有问题的标题,以便ipad打开它进行预览。

简单的php代理:

//-- Get the file url from request url 
//-- Should probably do some security checks on this but this is a simple example
$file = $_GET['file'];

//-- this essentially downloads the file, once again this is a simple example, you may want to use curl and write the file to /tmp otherwise large files will fail or crash your server (also curl is more reliable)
$file_contents = file_get_contents($file);

//-- we now have the file, so we set our custom headers (you will have to decide on this)
header('Content-Type: application/whatever');

//-- dump out the contents, since we have it in memory, we can echo the variable
//-- of writing to file via curl, you would read the file out to the browser at x bytes at a time until completed 
echo $file_contents;

注意:上面的确非常不安全,它只是为了展示一个简单的代理如何工作。你需要首先考虑安全问题。如果文件始终位于特定的第三方主机上,则对域进行硬编码,并仅允许文件路径为e.c.t.