0

ファイルを返すWebサービスがあり、タグにWebサービスのURLを入れたので、次のようになり <a href="webserviceurl/someurl"/>ます。リンクをクリックして、iPadのサファリでこれを開こうとすると、単に「サファリはこれをダウンロードできません」と表示されますファイル"。デスクトップでこのリンクを開いて応答ドキュメントを表示すると、ファイルのヘッダーは次のようになります。

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でファイルを完全にプレビューするように、ファイルへの直接リンクがあるURLを試したので、これが起こるのは「Content-Disposition」だと思います。

Web サービスを変更できません。変更できるのは JavaScript コードのみです。アプリケーションは angularJS で構築されています。iPad でファイルをプレビューするためにできる解決策はありますか?

4

1 に答える 1

0

私の知る限り、iOS サファリはファイルのダウンロードをサポートしていません。

したがって、これを解決する唯一の方法は、制御可能なプロキシ サーバーを使用することです。ここでは、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;

注: 上記は実際には非常に安全ではありません。単純なプロキシがどのように機能するかを示すためのものです。何よりもまずセキュリティについて考える必要があります。ファイルが常に特定のサード パーティ ホスト上にある場合は、ドメインをハードコーディングし、ファイル パスのみを指定できるようにします。

于 2013-06-11T09:37:06.003 に答える