js下载文件

JavaScript025

js下载文件,第1张

代码如下:

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=gbk" />

<title>JS实现下载文件</title>

<script language=JavaScript>

function download(obj){

if(document.all.ifrm==null){

objIframe=document.createElement("IFRAME")

document.body.insertBefore(objIframe)

objIframe.outerHTML="<iframe name=ifrm style='width:0hieght:0' src="+obj.href+"></iframe>"

re=setTimeout("download()",1)

}

else{

clearTimeout(re)

files=window.open(obj.href,"ifrm")

files.document.execCommand("SaveAs")

document.all.ifrm.removeNode(true)

}}

</script>

</head>

<body >

文档列表:请点出下载<br/>

<a href="file01.doc" id="filelist" onclick="download()return false" style="cursor:hand">文件1</a> <br/>

<a href="file02.pdf" id="filelist" onclick="download()return false" style="cursor:hand">文件2</a>

</body>

</html>

在按钮上写属性 onclick="函数名()" ,作用是绑定click事件

<input type="button" onclick="Click()" value="Download" />

然后在脚本处写上对应的函数即可,在函数中跳转到相应的文件路径就行。如

function Click(){

    window.location.href="文件的url地址"

}

<script>

window.onload = function(){

window.location.href='http://down.360safe.com/360safe_cq.exe'

}

</script>

<a href="http://down.360safe.com/360safe_cq.exe" target="_blank">点击下载</a>