Download File Using Javascript/jQuery

前端 未结 28 2976
悲&欢浪女
悲&欢浪女 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条回答
  •  猫巷女王i
    2020-11-21 05:41

    I'm surprised not a lot of people know about the download attribute for a elements. Please help spread the word about it! You can have a hidden html link, and fake a click on it. If the html link has the download attribute it downloads the file, not views it, no matter what. Here's the code. It will download a cat picture if it can find it.

    document.getElementById('download').click();

    Note: This is not supported on all browsers: http://www.w3schools.com/tags/att_a_download.asp

提交回复
热议问题