无论如何要为用户调出“打开/保存”对话框?

时间:2009-12-23 13:35:10

标签: php javascript html

不确定标题是否有意义抱歉...基本上我正在生成我想在客户端机器上自动打开的Word文档,但似乎这是不可能的,所以不要只是列出一个列表而已他们手动点击每一个,我想知道它是否至少可以提出'你想保存或打开这个文件'对话:(

远景,但我知道当你下载东西时会有很多网站会这样做...重定向下载服务器等

由于

3 个答案:

答案 0 :(得分:2)

您可以做的最好的事情是使用Content-disposition标头提供“提示”。

http://php.net/manual/en/function.header.php

指定类似

的内容
<?php
header('Content-type: application/msword');

header('Content-Disposition: attachment; filename="downloaded.pdf"');

// output content of document
?>

应该导致大多数浏览器提示用户下载文档,而这个:

header('Content-Dispotion: inline')

通常应该使浏览器在现有窗口中显示该文件。

答案 1 :(得分:1)

您可以设置contenttype和content-disposition标题以在浏览器中打开打开/保存对话框。

Header Field Definition

了解详情。

答案 2 :(得分:1)

您需要发送正确的标题,依此类推。

我发现this似乎很好:

<?php
function send_file($name) {
  ob_end_clean();
  $path = "protected/".$name;
  if (!is_file($path) or connection_status()!=0) return(FALSE);
  header("Cache-Control: no-store, no-cache, must-revalidate");
  header("Cache-Control: post-check=0, pre-check=0", false);
  header("Pragma: no-cache");
  header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
  header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
  header("Content-Type: application/octet-stream");
  header("Content-Length: ".(string)(filesize($path)));
  header("Content-Disposition: inline; filename=$name");
  header("Content-Transfer-Encoding: binary\n");
  if ($file = fopen($path, 'rb')) {
    while(!feof($file) and (connection_status()==0)) {
      print(fread($file, 1024*8));
      flush();
    }
    fclose($file);
  }
  return((connection_status()==0) and !connection_aborted());
}
?>

以下是使用该功能的示例:

<?php
if (!send_file("platinumdemo.zip")) {
die ("file transfer failed");
// either the file transfer was incomplete
// or the file was not found
} else {
// the download was a success
// log, or do whatever else
}
?>