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 matlab透明图叠加
  • ¥50 基于stm32l4系列 使用blunrg-ms的ble gatt 创建 hid 服务失败
  • ¥150 计算DC/DC变换器平均模型中的参数mu
  • ¥25 C语言代码,大家帮帮我
  • ¥15 请问以下文字内容及对应编码是用了什么加密算法或压缩算法呢?
  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验