超链接有效 - 带有“提交”按钮的 Html 表单无效

发布于 2024-07-11 08:48:28 字数 940 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愛上了 2024-07-18 08:48:28

我认为操作 url 已被编码,并且未发送硬编码的 GET 参数。

您可以尝试将操作网址简单地设置为 download.php 并有两个包含“file”和“properFilename”参数的隐藏字段,如下所示:

<form action="download.php" method="GET">
  <input type="submit" value="Download Audio" name="download"/>
  <input type="hidden" name="file" value="test.mp3" />
  <input type="hidden" name="properFilename" value="Testing File" />
</form>

或者您可以使用输入 type="button",即使没有表单:

<input type="button" 
      onclick="location.href='download.php?file=test.mp3&properFilename=Testing File';" 
      value="Download"/>

或者图像按钮:

<a href="download.php?file=test.mp3&properFilename=Testing File">
   <img src="IMAGE_BUTTON_HERE" />
</a>

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:

<form action="download.php" method="GET">
  <input type="submit" value="Download Audio" name="download"/>
  <input type="hidden" name="file" value="test.mp3" />
  <input type="hidden" name="properFilename" value="Testing File" />
</form>

Or you can use a input type="button", even without the form:

<input type="button" 
      onclick="location.href='download.php?file=test.mp3&properFilename=Testing File';" 
      value="Download"/>

Or an image button:

<a href="download.php?file=test.mp3&properFilename=Testing File">
   <img src="IMAGE_BUTTON_HERE" />
</a>
划一舟意中人 2024-07-18 08:48:28

而不是使用 正如CMS建议的那样,您还可以使用按钮元素(这实际上是语义正确的元素,因为您不在表单中)

<button onclick="document.location = 'bla.php?f=vars'">Download</button>

当然更好的方法是将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)

<button onclick="document.location = 'bla.php?f=vars'">Download</button>

Even better would be off course be to move the onclick event out of the button into a seperate javascript file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文