Skip to content

Instantly share code, notes, and snippets.

@nailuoGG
Created February 21, 2019 13:58
Show Gist options
  • Save nailuoGG/49f0dcec82aef762c1d778b5fff54e41 to your computer and use it in GitHub Desktop.
Save nailuoGG/49f0dcec82aef762c1d778b5fff54e41 to your computer and use it in GitHub Desktop.
用于点击链接下载文件
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