Download File Using Javascript/jQuery

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

    I know I'm late for the party, but I'd like to share my solution which is variation of Imagine Breaker's solution above. I tried to use his solution, because his solution seems most simple and easy to me. But like other said, it didn't work for some browsers, so I put some variation on it by using jquery.

    Hope this could help someone out there.

    function download(url) {
      var link = document.createElement("a");
      $(link).click(function(e) {
        e.preventDefault();
        window.location.href = url;
      });
      $(link).click();
    }
    

提交回复
热议问题