JS打开[保存文件对话框]出错:Automation server can't create object
本人想实现一个客户端js保存Table的数据,用户点击下载后,不提交到服务器,直接使用js读取数据后,打开保存对话框,用户输入路径和文件名然后下载。
遇到如下问题:
1、new ActiveXObject( "MSComDlg.CommonDialog ")
Error:Automation server can 't create object
(IE端,注册表没有问题,COMDLG32.OCX、comdlg32.dll均存在)
另外,new ActiveXObject( "WScript.Shell ")也是同样的错误,但是new ActiveXObject( "Scripting.FileSystemObject ")没有错误!
2、有可能是安全设置的问题么?如果是,如何修改安全设置(windows2003)
3、有其他的实现思路么?(只要不提交到服务器)
[解决办法]
3种可能:
1.在运行里面执行regsvr32 scrrun.dll,如果不行,继续;
2.安装msxml3.dll,再不行,还继续
3.在internet选项里"安全"选项卡中涉及到activex的都启用,安全级别设置为 中.再不行,我也没招了.
[解决办法]
用你这种方式当然会被浏览器的安全机制毙掉!
<input type=button value=保存 onclick= "document.execCommand( 'SaveAs ') " />
<input type=button value=另存为 onclick= "document.execCommand( 'Saveas ',false, 'c:\\test.htm ') " />
[解决办法]
那就用FileSystemObject写你想写的内容啊。
[解决办法]
梅花雪的思路可以实现LZ所需的效果,因为我也有用过,如保存EXCEL文件
[解决办法]
asp.jscript
<%@LANGUAGE= "JAVASCRIPT " CODEPAGE= "65001 "%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=utf-8 " />
<title> shawl.qiu template </title>
</head>
<body>
<%
Download( "id ", "file ", "/upload/ ", false);
function Download(QueryId, FilePath, pathForCheck, Debug)
{
var Debug = false;
var QueryId = Request.QueryString(QueryId)+ " ";
var FilePath = Request.QueryString(FilePath)+ " ";
if(Debug)
{
Response.Write( " <li/> typeof(QueryId): "+typeof(QueryId));
Response.Write( " <li/> QueryId: "+QueryId);
Response.Write( " <li/> QueryId==\ "\ ": "+(QueryId== " "));
Response.Write( " <li/> QueryId==\ "undefined\ ": "+(QueryId== "undefined "));
Response.Write( " <hr/> ");
Response.Write( " <li/> typeof(FilePath): "+typeof(FilePath));
Response.Write( " <li/> FilePath: "+FilePath);
Response.Write( " <li/> FilePath==\ "\ ": "+(FilePath== " "));
Response.Write( " <li/> FilePath==\ "undefined\ ": "+(FilePath== "undefined "));
Response.Write( " <hr/> ");
}
if(QueryId== "undefined "||FilePath== "undefined ")Response.End();
switch(QueryId)
{
case "download ":
if(Debug)
{
Response.Write( " <li/> download: ");
Response.Write( " <hr/> ");
}
if(!CheckPathIn(FilePath, pathForCheck, true, Debug))Response.End();
DownloadFile(FilePath, true);
break;
}
function CheckPathIn(path, pathForCheck, bCovPath, Debug)
{
if(bCovPath)
{
path=Server.MapPath(path);
pathForCheck=Server.MapPath(pathForCheck);
} // end if
if(Debug)
{
Response.Write( " <li/> path: "+path);
Response.Write( " <li/> pathForCheck: "+pathForCheck);
}
if(path.indexOf(pathForCheck) <0)return false;
return true;
}
function DownloadFile(sFilePath, bCovPath, Debug)
{
if(sFilePath== " ")return;
if(bCovPath)sFilePath=Server.MapPath(sFilePath);
if(Debug)
{
Response.Write( " <li/> download debug: ");
Response.Write( " <li/> sFilePath: "+sFilePath);
Response.End();
}
if(!fFlCkFl(sFilePath))return;
var fileName = sFilePath.replace(/.*\\/, " ");
Response.Clear();
Response.ContentType= "application/octet-stream ";
Response.AddHeader( "Content-Disposition ", "attachment;filename= "+fileName);
Response.BinaryWrite(fReadBinaryFromFile(sFilePath));
Response.Flush();
Response.End();
function fFlCkFl(sPath){
return new ActiveXObject( "scripting.fileSystemObject ").FileExists(sPath);
}
function fReadBinaryFromFile(sPath, sCharset){
var o=new ActiveXObject( 'adodb.stream ')
with(o){
Type=1;
Mode=3;
Open();
LoadFromFile(sPath);
var pNum=0
if(typeof sCharset!= 'undefined '&&sCharset!= ' '){
CharSet=sCharset;
if(sCharset== 'utf-8 '||sCharset== 'unicode ')pNum=2;
}
Position=pNum;
var $str=Read();
close();
}
o=null;
return $str;
} // end function fReadBinaryFromFile(sPath, sCharset) // shawl.qiu code
} // end function DownloadFile // shawl.qiu code
} // end function Download
%>
</body>
</html>
[解决办法]
学习
[解决办法]
你可以先 var win = window.open(); 然后再 win.document.write(str), 把你的内容写到这个新开窗口中,然后再 win.document.execCommand( 'Saveas ',false, 'c:\\test.htm '), 最后 win.close() 关掉这个临时窗口。