超链接有效 - 带有“提交”按钮的 Html 表单无效
我有一个 PHP 脚本,可以推送标头以允许下载文件。 当通过超链接或使用链接通过浏览器调用时,该脚本可以正常工作。 它看起来是这样的:
<a href="download.php?file=test.mp3&properFilename=Testing File">Download</a>
我希望这是一个按钮(sbumit),所以我这样做了:
<form action="download.php?file=test.mp3&properFilename=Testing File" method="get">
<input type="submit" value="Download Audio" name="download"/>
</form>
但是,这不起作用。 当我点击它时。 它启动下载对话框,但文件名为空。 它显示文件名为“.mp3”(不带引号)! 通过超链接的同一链接显示了确切的文件名“测试文件”。 为什么是这样?? 以下是相关的 PHP 代码片段:
$filename = '../'.$_GET['file'];
$properFilename = $_GET['properFilename'].'.mp3';
header("Content-Disposition: attachment; filename=\"".basename($properFilename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
感谢您的帮助。 这让我一天一夜都抓狂!!!
I have a PHP script that pushes the headers to allow a file to download. This script works fine when it called via a hyperlink or through the browser using link. This is how it looks like:
<a href="download.php?file=test.mp3&properFilename=Testing File">Download</a>
I want this to be a button (sbumit) instead, so I did this:
<form action="download.php?file=test.mp3&properFilename=Testing File" method="get">
<input type="submit" value="Download Audio" name="download"/>
</form>
However, this doesn't work. When I click on it. It initiates the download dialog box but the filename is empty. It shows file name as ".mp3" (without quotes)! That same link via the hyperlink shows the exact file name "Testing File". Why is this?? Here is the PHP snippet concerned:
$filename = '../'.$_GET['file'];
$properFilename = $_GET['properFilename'].'.mp3';
header("Content-Disposition: attachment; filename=\"".basename($properFilename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
Thank you for any help. This has been driving me mad all day and night!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为操作 url 已被编码,并且未发送硬编码的 GET 参数。
您可以尝试将操作网址简单地设置为 download.php 并有两个包含“file”和“properFilename”参数的隐藏字段,如下所示:
或者您可以使用输入 type="button",即使没有表单:
或者图像按钮:
I think that the action url it's been urlencoded, and the hard-coded GET parameters are not sent.
You can try to set the action url simply to download.php and have two hidden fields containing the "file" and "properFilename" parameters, like this:
Or you can use a input type="button", even without the form:
Or an image button:
而不是使用 正如CMS建议的那样,您还可以使用按钮元素(这实际上是语义正确的元素,因为您不在表单中)
当然更好的方法是将onclick事件从按钮移到单独的javascript文件中。
Instead of using <input type='button'> as CMS suggests, you can also use the button element (which is actually the semantic correct one here since your not in a form)
Even better would be off course be to move the onclick event out of the button into a seperate javascript file.