ほげほげ

プログラミング、英会話、ヨガ、料理などの備忘録など。

画面遷移しないでファイルダウンロード

サーバーから適切なContent-typeを指定してあげれば、location.hrefの設定のみでよい。

php

        header('Status: 200 OK');
        header('Content-type: application/octet-stream');
        header('Content-Disposition: attachment; filename="hoge.txt"'); // ファイル名

javascript

$('#btn").click(function(){
  location.href = 'download_page.php' ;
}) ;