昨天写了好几个这样的函数,每个都采用了不同的办法来实现,这两个是比较完美的,个人感觉还是SaveAs5最好,简洁优美。
这两函数共同存在的问题,在IE6 SP1下跨域引用的图片会出错。IE的安全机制问题,估计没法解决了。![]()
function SaveAs4(imgURL)
{
var oPop = window.open("","","width=1, height=1, top=5000, left=5000");
oPop.document.open();
oPop.document.writeln("<script>");
oPop.document.writeln("function SaveAs(){document.frames('tempFrame').document.execCommand('SaveAs');self.close()}");
oPop.document.writeln("</script>");
oPop.document.writeln("<iframe src='" + imgURL + "' id='tempFrame' onload='SaveAs()'></iframe>");
oPop.document.writeln("<input type='button' value='download' onClick='SaveAs()'>");
oPop.document.close();
}
function SaveAs5(imgURL)
{
var oPop = window.open(imgURL,"","width=1, height=1, top=5000, left=5000");
for(; oPop.document.readyState != "complete"; )
{
if (oPop.document.readyState == "complete")break;
}
oPop.document.execCommand("SaveAs");
oPop.close();
}
