使用window.navigator.userAgent获取浏览器你内核,然后判断是不是ie/edge内核
// 判断浏览器类型
function myBrowser() {
var sBrowser = null
var sUsrAg = window.navigator.userAgent
if (sUsrAg.indexOf('Firefox') > -1) {
sBrowser = 'FF'
} else if (sUsrAg.indexOf('Opera') > -1 || sUsrAg.indexOf('OPR') > -1) {
sBrowser = 'Opera'
} else if (sUsrAg.indexOf('Trident') > -1) {
sBrowser = 'IE'
} else if (sUsrAg.indexOf('Edge') > -1) {
sBrowser = 'Edge'
} else if (sUsrAg.indexOf('Chrome') > -1) {
sBrowser = 'Chrome'
} else if (sUsrAg.indexOf('Safari') > -1) {
sBrowser = 'Safari'
} else {
sBrowser = 'unknown'
}
return sBrowser
}
如果是ie/edge内核
// IE浏览器图片保存本地
function SaveAs5(url, fileName) {
var bstr = atob(url.split(',')[1])
var n = bstr.length
var u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
var blob = new Blob([u8arr])
window.navigator.msSaveOrOpenBlob(blob, fileName)
}
其他浏览器
// 谷歌,360极速等浏览器下载
function download(src, fileName) {
var aLink = document.createElement('a');
aLink.download = fileName;
aLink.href = src
aLink.click();
};
完整js代码
// 下载前先判断浏览器的内核
function oDownLoad(url, fileName) {
if (myBrowser() == "IE" || myBrowser() == "Edge") {
SaveAs5(url, fileName);
} else {
download(url, fileName);
}
}
// IE浏览器图片保存本地
function SaveAs5(url, fileName) {
var bstr = atob(url.split(',')[1])
var n = bstr.length
var u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
var blob = new Blob([u8arr])
window.navigator.msSaveOrOpenBlob(blob, fileName)
}
// 谷歌,360极速等浏览器下载
function download(src, fileName) {
//需要转成blob,base64下载大图会超出href的限制,导致下载失败
var aLink = document.createElement('a');
aLink.download = fileName;
// var blob = dataURLtoBlob(src);
aLink.href = src // URL.createObjectURL(blob);
aLink.click();
};
// 判断浏览器类型
function myBrowser() {
var sBrowser = null
var sUsrAg = window.navigator.userAgent
if (sUsrAg.indexOf('Firefox') > -1) {
sBrowser = 'FF'
} else if (sUsrAg.indexOf('Opera') > -1 || sUsrAg.indexOf('OPR') > -1) {
sBrowser = 'Opera'
} else if (sUsrAg.indexOf('Trident') > -1) {
sBrowser = 'IE'
} else if (sUsrAg.indexOf('Edge') > -1) {
sBrowser = 'Edge'
} else if (sUsrAg.indexOf('Chrome') > -1) {
sBrowser = 'Chrome'
} else if (sUsrAg.indexOf('Safari') > -1) {
sBrowser = 'Safari'
} else {
sBrowser = 'unknown'
}
return sBrowser
}
// 直接引用
oDownLoad(base64文件,'aa.jpg')
dataURLtoBlob 你们可能用不了,想其他方法转吧
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。