【发布时间】:2009-12-23 13:35:09
【问题描述】:
不确定标题是否有意义抱歉...基本上我正在生成我想在客户端机器上自动打开的 Word 文档,但似乎这是不可能的,所以不要只显示一个列表并拥有他们手动点击每一个,我想知道它是否至少可以弹出“你想保存还是打开这个文件”对话框:(
远射,但我知道很多网站会在您下载内容时这样做...重定向到下载服务器等
谢谢
【问题讨论】:
标签: php javascript html
不确定标题是否有意义抱歉...基本上我正在生成我想在客户端机器上自动打开的 Word 文档,但似乎这是不可能的,所以不要只显示一个列表并拥有他们手动点击每一个,我想知道它是否至少可以弹出“你想保存还是打开这个文件”对话框:(
远射,但我知道很多网站会在您下载内容时这样做...重定向到下载服务器等
谢谢
【问题讨论】:
标签: php javascript html
您能做的最好的事情就是在 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')
通常应该使浏览器在现有窗口中显示文件。
【讨论】:
【讨论】:
您需要发送正确的标头等。
我发现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
}
?>
【讨论】: