Created
February 21, 2019 13:58
-
-
Save nailuoGG/49f0dcec82aef762c1d778b5fff54e41 to your computer and use it in GitHub Desktop.
用于点击链接下载文件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const downloadFile = (fileName, url) => { | |
if (isIE()) { | |
ieDown(url) | |
} else { | |
const aLink = document.createElement('a'); | |
const evt = document.createEvent('MouseEvents'); | |
// var evt = document.createEvent("HTMLEvents") | |
evt.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); | |
aLink.download = fileName; | |
aLink.href = url; | |
aLink.dispatchEvent(evt) | |
} | |
}; | |
const ieDown = url => { | |
window.open(url) | |
}; | |
const isIE = () => { | |
const explorer = window.navigator.userAgent; | |
return explorer.indexOf('MSIE') >= 0 || explorer.indexOf('Trident/7.0') >= 0 || explorer.indexOf('Edge') >= 0; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment