Download File Using Javascript/jQuery

前端 未结 28 2975
悲&欢浪女
悲&欢浪女 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
    闹比i (楼主)
    2020-11-21 05:49

    I use @rakaloof's solution without JQuery (because you don't need it here). Thank you for the idea! Here is a vanillaJS form-based solution:

    const uri = 'https://upload.wikimedia.org/wikipedia/commons/b/bb/Test_ogg_mp3_48kbps.wav';
    let form = document.createElement("form");
    form.setAttribute('action', uri);
    document.body.appendChild(form);
    form.submit();
    document.body.removeChild(document.body.lastElementChild);

提交回复
热议问题