当点击apply(submit) button时候会执行php端code (下载一个文件),工作正常。但是点击Link的时候没有下载。我写了个js 用ajax 的 xmlHttp.open 来模拟apply 功能。
有谁帮我看看我的code 谢谢啊! 附件是所有的code
js code: dl.js
------------------------------------
var xmlHttp = false;
function DownLoadFile()
{
if (xmlHttp != false)
{
var req = "dl.php";
xmlHttp.open( "GET", req, true );
xmlHttp.send( null );
//alert("Download File Dialog");
}
}
function Load ()
{
if (xmlHttp != false)
return;
if ((!xmlHttp) && (typeof XMLHttpRequest != 'undefined'))
xmlHttp = new XMLHttpRequest();
else
{
try
{
xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP" );
}
catch( e )
{
try
{
xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );
}
catch( e2 )
{
xmlHttp = false;
}
}
}
}
________________________________________________________
php code: dl.php
<?php
$file_dir = 'abc.txt';
downfile($file_dir);
function downfile($f_name)
{
if(file_exists($f_name))
{
header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
header("Content-Description:File Transfer");
header ("Content-type: application/octet-stream");
header ("Content-Length: " . filesize ($f_name));
header ("Content-Disposition: attachment; filename=" . basename($f_name));
readfile($f_name);
}
else
{
echo("No report found");
exit;
}
}
?>
_____________________________________________________________
html code: dl.html
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/maintemplate.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>download test</title>
<script language="javascript" src="dl.js">
</script>
<script language="javascript">
window.onload = function(){
Load();
}
</script>
</head>
<body>
<form action="dl.php" method="post" name="dl">
<a href="#" onClick="DownLoadFile();">Download a File </a>
<input type="submit" style="width:80px;height:42px;" name="Apply" value="Apply" />
</form>
</body>
</html>
--
修改:appleypp FROM 123.115.255.*
FROM 123.115.255.*
附件(1.7KB) download.zip