[英]How to make a link to download a server file on the browser?
I am trying to create a simple download link我正在尝试创建一个简单的下载链接
The file I want to download is a .pdf
that the user can upload, and now I want to make it possible to download.我想下载的文件是用户可以上传的
.pdf
,现在我想让下载成为可能。 With the pop-up box, you know, or just on the chrome download bar使用弹出框,你知道,或者只是在 chrome 下载栏上
I've tried what every answer says:我已经尝试过每个答案所说的:
<a href="<?=$GUIDELINES_PDF_DIR . $projectId . '.pdf'?>" download="guidelines.pdf">Download PDF</a>
But that leads to "Failed - no file", even though when I check但这会导致“失败 - 没有文件”,即使我检查
if(file_exists($GUIDELINES_PDF_DIR . $projectId . '.pdf'))
it returns true!它返回真!
Some other notes:其他一些注意事项:
I'm pretty shaky on server things, so maybe the same link doesn't work on PHP and on HTML?我对服务器的事情很不稳定,所以也许相同的链接在 PHP 和 HTML 上不起作用? In that case, how can I find the file?
在这种情况下,我如何找到该文件?
This link is within a <form>
, so I don't really want to create another <form>
inside it, or do anything that compromises the info that the user has inputed此链接位于
<form>
,所以我真的不想在其中创建另一个<form>
,或者做任何会损害用户输入信息的事情
Thank so much :)非常感谢 :)
$GUIDELINES_PDF_DIR
is a directory on your server's hard disk. $GUIDELINES_PDF_DIR
是服务器硬盘上的目录。
The resulting path you are creating is relative to the root of your server's hard disk and not to the DocumentRoot of your web server.您创建的结果路径与服务器硬盘的根目录相关,而不是与 Web 服务器的 DocumentRoot 相关。
You need to account for that difference when generating your URL.您需要在生成 URL 时考虑这种差异。
Use a right headers:使用正确的标题:
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='downloaded.pdf'");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.