PHP下載文件的方法 ajax也是用這種方式 鏈接跳轉就可以了
public function xiazai() { $file_path = __DIR__ . '/../../../Public/qitan.pdf'; $fp = fopen($file_path, "r"); $file_size = filesize($file_path); //下載文件需要用到的頭 Header("Content-type: application/octet-stream"); Header("Accept-Ranges: bytes"); Header("Accept-Length:" . $file_size); Header("Content-Length:" . $file_size); Header("Content-Disposition: attachment; filename=qitan.pdf"); $buffer = 1024; $file_count = 0; //向瀏覽器返回數據 while (!feof($fp) && $file_count < $file_size) { $file_con = fread($fp, $buffer); $file_count += $buffer; echo $file_con; } fclose($fp); }