Download File Using Javascript/jQuery

前端 未结 28 2931
悲&欢浪女
悲&欢浪女 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:52

    function downloadURI(uri, name) 
    {
        var link = document.createElement("a");
        // If you don't know the name or want to use
        // the webserver default set name = ''
        link.setAttribute('download', name);
        link.href = uri;
        document.body.appendChild(link);
        link.click();
        link.remove();
    }
    

    Check if your target browser(s) will run the above snippet smoothly:
    http://caniuse.com/#feat=download

提交回复
热议问题