Launch download in the same tab without opening new tab or window in Javascript

后端 未结 4 544
野趣味
野趣味 2020-12-13 06:38

I am using this javascript function to launch download

function startDownload(url) {
   window.open(url, \'Download\');
}

It works, but i

4条回答
  •  星月不相逢
    2020-12-13 07:29

    function startDownload(url) {
    
        window.location.href = url;
    }
    

    This will start the download in the same page, exactly like when you click a link without any target other than _self.

    To force the download of a file, make sure you send the right headers with it:

    Content-Disposition: attachment; filename="mypdf.pdf";
    

    This will make sure that the file is not displayed in the browser instead of being downloaded. Replace the filename part with the filename you want as default on the save as dialog.

提交回复
热议问题