真的查阅了很多资料,目前知道需要下载文件的url。需要纯前端下载文件,使用a标签的方法,对于浏览器会直接打开,而不是下载文件,而download属性不支持ie11. 问个纯前端下载文件的方法,可兼容现代浏览器,兼容IE11和以上版本。
真的查阅了很多资料,目前知道需要下载文件的url。需要纯前端下载文件,使用a标签的方法,对于浏览器会直接打开,而不是下载文件,而download属性不支持ie11. 问个纯前端下载文件的方法,可兼容现代浏览器,兼容IE11和以上版本。
把我们项目中使用的代码直接给你吧!
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;
};
10 回答11.7k 阅读
2 回答3.2k 阅读✓ 已解决
3 回答1.7k 阅读✓ 已解决
5 回答804 阅读
4 回答2.2k 阅读✓ 已解决
3 回答1.2k 阅读✓ 已解决
3 回答840 阅读✓ 已解决
download.js