Download File Using Javascript/jQuery

前端 未结 28 2876
悲&欢浪女
悲&欢浪女 2020-11-21 05:11

I have a very similar requirement specified here.

I need to have the user\'s browser start a download manually when $(\'a#someID\').click();

But

28条回答
  •  太阳男子
    2020-11-21 05:55

    To improve Imagine Breaker 's answer, this is supported on FF & IE :

    var evt = document.createEvent("MouseEvents");
    evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    
    function downloadURI(uri, name) {
        var link = document.createElement("a");
        link.download = name;
        link.href = uri;
        link.dispatchEvent(evt);
    }
    

    In other words, just use a dispatchEvent function instead of click();

提交回复
热议问题