保證在瀏覽器上word/圖片/Excel的下載的表現形式一樣
阿新 • • 發佈:2018-05-11
nbsp setattr ref bsp TP fir attribute 自動 exc
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);
}
結論,谷歌可以,但是火狐不行,還是坑.......
$a.setAttribute("href", src); $a.setAttribute("download", ""); $a.setAttribute("target", "_parent"); //var evObj = document.createEvent(‘MouseEvents‘);
保證在瀏覽器上word/圖片/Excel的下載的表現形式一樣