admin管理员组文章数量:1530279
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS实现文件上传下载</title>
</head>
<body>
<a href="javascript:void(0);" id="oDownLoad" οnclick="oDownLoad('1.pdf','oDownLoad')">下载</a>
</body>
<script>
function myBrowser(){
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
var isOpera = userAgent.indexOf("Opera") > -1;
if (isOpera) {
return "Opera"
}; //判断是否Opera浏览器
if (userAgent.indexOf("Firefox") > -1) {
return "FF";
} //判断是否Firefox浏览器
if (userAgent.indexOf("Chrome") > -1){
return "Chrome";
}
if (userAgent.indexOf("Safari") > -1) {
return "Safari";
} //判断是否Safari浏览器
if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
return "IE";
}; //判断是否IE浏览器
if (userAgent.indexOf("Trident") > -1) {
return "Edge";
} //判断是否Edge浏览器
}
function oDownLoad(url,id) {
if (myBrowser()==="IE" || myBrowser()==="Edge"){
var oPop = window.open(url,"","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();
}else{
//!IE
var odownLoad=document.getElementById(id);
odownLoad.href=url;
odownLoad.download="";
}
}
</script>
</html>
注:在html同一目录下准备一个1.pdf文件。
版权声明:本文标题:JS实现文件下载(适应多种浏览器) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1726114727a1055976.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论