search

Home  >  Q&A  >  body text

The easiest way: open the download window on the page without leaving the page

<p>What is the best way to open a download dialog in various browsers without leaving the current page or opening a popup (assuming we can set content-disposition:attachment in the header) Problems will occur in Internet Explorer (IE) 6. </p>
P粉432906880P粉432906880463 days ago522

reply all(2)I'll reply

  • P粉803527801

    P粉8035278012023-08-24 16:14:42

    This javascript is fine, it doesn't open a new window or tab.

    window.location.assign(url);

    reply
    0
  • P粉864872812

    P粉8648728122023-08-24 13:31:41

    It's been 7 years and I don't know if it works in IE6, but it pops up the OpenFileDialog in FF and Chrome.

    var file_path = 'host/path/file.ext';
    var a = document.createElement('A');
    a.href = file_path;
    a.download = file_path.substr(file_path.lastIndexOf('/') + 1);
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);

    reply
    0
  • Cancelreply