Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在iPad上预览safari中的文件?_Javascript_Ipad_Angularjs_Safari - Fatal编程技术网

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

Javascript 如何在iPad上预览safari中的文件?,javascript,ipad,angularjs,safari,Javascript,Ipad,Angularjs,Safari,我有一个返回文件的web服务,我将web服务url放在标记中,因此它变成: ,当我点击链接并尝试在iPad上的safari中打开此文件时,它只会告诉我“safari无法下载此文件”。当我在桌面上打开此链接并看到响应文档时,文件的标题如下所示: HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Content-Disposition: attachment; filename="test.doc" Content-Type: application/x-msdow

我有一个返回文件的web服务,我将web服务url放在标记中,因此它变成:
,当我点击链接并尝试在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
我猜是“内容配置”导致了这种情况,因为我尝试了一个直接链接到文件的url,比如
,它可以在iPad上完美地预览文件


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

据我所知,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.

谢谢您的回复,这当然是真的,顺便说一句,我设法使用angularJS的拦截器概念获得了具有头和体的响应dom,有没有办法用这些打开文件,你有什么可以推荐的代理服务器技术吗?我从来没有真正使用过angularJS,所以我不能说关于拦截器的事情。即使有了这些数据,我也想不出一种方法让iPad打开它,因为你需要safari浏览器来读取响应标题,检测文件并将其作为预览打开。除非您可以在JS中伪造一个响应并将数据放入响应体中,否则这是没有用的(我认为这是不可能的,因为在调用客户端JS之前读取了头)。关于php代理脚本,您可以自己构建一个,请参见上面第5章我的主要帖子mins@hiphop101好的,我添加了一些非常简单的东西,希望您对PHP理解得足够多,但它相对简单,您可以在PHP服务器上下载文件,设置与ipad兼容的自定义标题,然后将文件转储回ipad。还有一些安全性和内存问题需要考虑,但希望你能解决这些问题,或者在谷歌上搜索“php下载脚本”之类的东西,这是脚本所显示的内容的一半,我可以使用Javascript和C#API实现预览吗?