简体   繁体   English

如何制作链接以在浏览器上下载服务器文件?

[英]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.

相关问题 是否可以在远程服务器上创建文件的下载链接? - Is it possible to make a download link for file on remote server? 单击时如何在新的浏览器选项卡中打开下载链接并在文件下载链接出现之前在该选项卡上设置倒计时 - How do I make a download link open in a new browser tab when clicked and set a countdown on that tab before the file download link appears 下载应用程序内浏览器的文件链接 - Download File Link for In App Browser xpages中的Lotus Notes客户端链接和浏览器链接以下载存储在服务器目录中的文件 - Lotus notes client link and browser link in xpages to download file stored in server directory 如何让浏览器使用angular Resource下载文件? - How to make browser to download file using angular Resource? 如何使浏览器从xhr请求中下载文件 - How to make browser download file from xhr request 单击服务器上的下载链接后如何删除下载的文件 - How to remove the downloaded file after clicking on the download link from server 如何制作图像链接,询问用户是否要下载,而不是在浏览器中显示 - how to make an image link ask the user if they want to download it, instead of displaying it in browser 如何使用 ReactJS 在浏览器中链接下载为 PDF - How to link for download as PDF in browser using ReactJS 如何使用浏览器下载管理器从 express + mongoDB 服务器下载/流式传输文件 - How to download/stream a file from a express + mongoDB server, using the browser download manager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM