在html或者jsp中d出文件另存为的对话框,可以调用document.execCommand方法
调用JS方法document.execCommand即可在前台页面调出另存为对话框,并将将服务器上存放的文件保存到本地。HTML页面另存为对话框DEMO.rar
代码:<script language='javascript' type='text/javascript'>
function getimg() //另存为存放在服务器上图片到本地的方法
{
event.returnValue=false
show.window.location.href=imgSrc.src
timer=setInterval(checkload,100)
}
function checkload()
{
if(show.readyState!="complete")
{
//调用document.execCommand方法,'Saveas'表示打开文件另存为对话框命令
show.document.execCommand('SaveAs')
clearInterval(timer)
}
}
</script>
<img id="imgSrc" width="320px" height="200px" src="test.bmp" alt=""/></br>
<input id="btnSaveAs" type="button" value="另存为上图" onclick="getimg()"/>
<iframe src="" name="show" style="width:0height:0"></iframe>
<!--图片另存为---><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title$amp>amp$nbsp通过js保存图片到本地 </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script src="" type="text/javascript"$amp>amp$lt/script>
<script>
function DownLoadReportIMG(imgPathURL) {
//如果隐藏IFRAME不存在,则添加
if (!document.getElementById("IframeReportImg"))
$('<iframe id="IframeReportImg" name="IframeReportImg" onload="DoSaveAsIMG()" width="0" height="0" src="about:blank"$amp>amp$lt/iframe$amp>apos$).appendTo("body")
if (document.all.IframeReportImg.src != imgPathURL) {
//加载图片
document.all.IframeReportImg.src = imgPathURL
}
else {
//图片直接另存为
DoSaveAsIMG()
}
}
function DoSaveAsIMG() {
if (document.all.IframeReportImg.src != "about:blank")
document.frames("IframeReportImg").document.execCommand("SaveAs")
}
</script>
</head>
<body>
<input type="button" value="download image" onclick="DownLoadReportIMG('')"$amp>amp$nbsp
</body>
</html>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)