window.open无法在IE中打开

时间:2012-01-16 20:19:22

标签: javascript internet-explorer-8

我正在尝试使用window.open打开word文档,如下所示

window.open("myworddoc.doc");

它在FF中工作正常,但IE尝试打开一个选项卡,但立即关闭它并跳回当前屏幕(不显示任何对话框以保存或打开文件)。

可能是什么问题?

2 个答案:

答案 0 :(得分:4)

这肯定是一个安全问题。使用JavaScript打开Word文档可能会产生令人讨厌的影响。想象一下,如果您正在浏览互联网,并且有人在您的网页加载时打开受感染的Word文档。

就个人而言,我会创建一个PHP文件,让我们说“fedoc.php”,然后像这样打开该文件:

window.open("servedoc.php");

serveoc.php可能包含以下内容:

<?php

$file = "myworddoc.doc"; 

header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/msword");
header("Content-Transfer-Encoding: binary");

readfile($file);

IE将打开PHP文件,因为它是一个完全有效的Web文件。 PHP脚本将文件提供给浏览器,要求用户下载文件。

答案 1 :(得分:0)

或者如果您使用的是.net(vb):

Response.ContentType = "image/jpeg" 'mime type of the file to serve.
Response.AddHeader("content-Disposition", "attachment;filename=YOURFILENAME")
Response.TransmitFile(YourFILEPath)

如果您愿意,可以让他们下载.doc或.zip文件。

相关问题