亲测可行,拿走不谢,只兼容ie10以上
index.html
<!DOCTYPE html>
<html>
<head>
<title>IE如何下载图片</title>
<script>
function download() {
var self = this
self.formatTimestamp = function () {
var timestamp = new Date()
var year = timestamp.getFullYear()
var date = timestamp.getDate()
var month = ('0' + (timestamp.getMonth() + 1)).slice(-2)
var hrs = ('0' + timestamp.getHours()).slice(-2)
var mins = ('0' + timestamp.getMinutes()).slice(-2)
var secs = ('0' + timestamp.getSeconds()).slice(-2)
var ms = timestamp.getMilliseconds()
return year + '-' + month + '-' + date + ' ' + hrs + ':' + mins + ':' + secs + '.' + ms
}
// 文件地址
self.logFilename = 'https://images2015.cnblogs.com/blog/397065/201509/397065-20150929204829605-1367621558.png'
var downloadFileName = self.formatTimestamp() + '-' + self.logFilename
// 关键是这个方法,ie自己提供的东西,但是好像只兼容到ie10,下面那个方法我也不知道行不行。
if (window.navigator.msSaveBlob) {
try {
var blobObject = new Blob([self.output])
window.navigator.msSaveBlob(blobObject, downloadFileName)
console.log(123)
}catch (e) {
console.log(e)
}
}else {
var file = 'data:text/plain;charset=utf-8,'
var logFile = self.output
var encoded = encodeURIComponent(logFile)// 这是个js自带的方法,具体你还是百度吧
file += encoded
var a = document.createElement('a')
a.href = file
a.target = '_blank'
a.download = downloadFileName
document.body.appendChild(a)
a.click()
a.remove()
}
}
</script>
</head>
<body>
<button onclick="download()">下载图片</button>
</body>
</html>
网友评论