保证在浏览器上word/图片/Excel的下载的表现形式一样

function downloadImage(src) {
console.log(src);
//src="http://192.168.12.50:8181/file/common/png/info_1526003595250.png";
var $a = document.createElement('a');

$a.setAttribute("href", src);
$a.setAttribute("download", "");
$a.setAttribute("target", "_parent");
 
//var evObj = document.createEvent('MouseEvents');

if( document.createEvent )
{
var evObj = document.createEvent('MouseEvents');
evObj.initMouseEvent( 'click', false, false, window, 0, 0, 0, 0, 0, true, true, true, true, 0, null);
$a.dispatchEvent(evObj);
}
else if( document.createEventObject ){
$a.fireEvent('click');
}
 
//下面这些参数我也不知道是干嘛的,只知道第五个零後面的参数,设置为true的话会新建标签页下载完成后自动关闭
 
//设为false的话下载完成后不关闭
 
//evObj.initMouseEvent( 'click', false, false, window, 0, 0, 0, 0, 0, true, true, true, true, 0, null);
 
//$a.dispatchEvent(evObj);
}
 
结论,谷歌可以,但是火狐不行,还是坑.......
原文地址:https://www.cnblogs.com/cx709452428/p/9025349.html