download file using an ajax request

后端 未结 12 2352
旧时难觅i
旧时难觅i 2020-11-21 23:46

I want to send an \"ajax download request\" when I click on a button, so I tried in this way:

javascript:

var xhr = new XMLHttpRequest();
xhr.open(\         


        
12条回答
  •  -上瘾入骨i
    2020-11-22 00:47

    You actually don't need ajax at all for this. If you just set "download.php" as the href on the button, or, if it's not a link use:

    window.location = 'download.php';
    

    The browser should recognise the binary download and not load the actual page but just serve the file as a download.

提交回复
热议问题