dongluan6784 2016-03-06 11:15
浏览 18
已采纳

PHP / HTML:下载处理页面加载的PDF链接

I'm using following code

<?php
$file = 'COMPANY_PROFILE.pdf';

if (! file) {
    die('file not found'); //Or do something 
} else {
    // Set headers
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=$file");
    header("Content-Type: application/zip");
    header("Content-Transfer-Encoding: binary");
    // Read the file from disk
    readfile($file); 
}

This automatically gives a download file prompt and if i use something like this:

<a href="COMPANY_PROFILE.pdf" target="_blank" ">Download PDF </a>

it opens the PDF in the browser, so how do I fix this issue in a way that file gets downloaded on button click?

  • 写回答

1条回答 默认 最新

  • dta25920 2016-03-06 11:39
    关注

    You can achieve your desired results by different methods. In this way what you are doing right know you are telling php to look for a file if it doesn't exist just show that file doesn't exit other wise show document that exist.

    But what you are looking for should perform on some kind of an event. e.g Button Click.

    You can achieve this by these kind of method.

    AJAX Call

    By an ajax call on click on a button to go to this php function.

    Posting PHP

    Either you can post some thing to PHP and in your PHP code you can tell your code if this specific name has been posted then download pdf otherwise not to.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化