【问题标题】:How can I create an URL to make a file downloadable automatically using HTML and PHP?如何创建 URL 以使用 HTML 和 PHP 自动下载文件?
【发布时间】:2019-09-12 20:39:56
【问题描述】:

我的问题:

我希望能够向我的客户发送mywebsite.com/products 之类的链接,以便他们可以下载.xlsx 文件并查看我在自动取款机上看到的内容。

有什么方法可以创建像“www.mywebsite.com/file”这样的链接,这样当用户访问此 URL 时,浏览器会打开提示下载.xlsx 文件?

这是我的products.html 文件:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Refresh" content="0; url=http://mywebsite.com/products.xlsx"/>
  </head>
</html>

我已经将products.xlsx 上传到我的服务器。

我注意到如果我使用扩展名.html 访问该网站,它会提示用户下载文件。如果我在没有扩展程序的情况下访问,则会导致 404 错误。

也许是我的.htaccess 配置导致了这种情况?

也许是Options +MultiViewsHTML RewriteCond..

Options All -Indexes

DirectoryIndex index.php index.htm index.html

RewriteEngine on
RewriteBase /

# ---- Make pages render without their extension in the url
Options +MultiViews


# Force HTTPS on the subdomains/subdirectories login or admin
#RewriteCond %{HTTPS} off
#RewriteCond %{HTTP_HOST} ^(login|admin)\. [NC]
#RewriteCond %{REQUEST_URI} ^(login|admin)\. [NC,OR]
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Force WWW if no subdomain is given
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Remove php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

# Remove html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

我该如何解决这个问题?

【问题讨论】:

  • 是的,一个简单的锚标签应该可以做到这一点
  • 嗯..你能解释一下吗?在我的index.html 文件中,我在按钮中使用以下代码来提示用户下载文件:&lt;p&gt;&lt;a href="products.xlsx" download class="btn btn-common wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="2000ms"&gt;See Products&lt;/a&gt;&lt;/p&gt;。如何设置此代码的锚点并修改我的products.html 文件以实现所需的输出? (“自动”提示用户下载文件的 URL)
  • 这些类混淆了该元素上的问题,因为它们使 Bootstrap 以不同的方式对待 &lt;a&gt;。只需使用简单直接的&lt;a href="something.xlsx"&gt;Click here&lt;/a&gt;
  • 好的,&lt;a&gt;&lt;/a&gt; 属性并不是我的问题。用户可以单击我的按钮并下载文件。我想要实现的是向我的客户发送marketable URL,当他们访问该 URL 时,我的代码会提示它下载文件……而“可销售的 url”是指我不想发送给他们像“mywebsite.com/products.html”这样的链接。我想发送类似“mywebsite.com/products”的内容。

标签: php html .htaccess redirect download


【解决方案1】:

dot.Py 您是否希望 products.xlsx 文件的链接根据您与谁交谈而改变? IE。一个客户可能有一个包含某些信息的特定 products.xlsx 文件,但另一个客户可能有他们需要查看的完全不同的信息。

如果不是这样,您可以使用&lt;a&gt; 标记来引用链接。

例如:


<a href='/excel/products.xlsx' target="_blank">Click to Download Excel Document</a>

那么您需要做的就是将其包含在页面上的&lt;body&gt; 中,客户可以单击链接下载文件。

【讨论】:

  • 我不需要根据客户端更改文件。我需要让我的客户点击链接“website.com/download”,然后它会自动提示用户下载文件。
  • 啊,在这种情况下,只需让您的 url 指向您文件夹中的文件位置。例如,如果您将 index.html 旁边的文件保存在名为 excel 的文件夹中,您将从 url 中删除 index.html 并将其替换为 /excel/products.xlsx。这会自动触发下载。
【解决方案2】:

如果您想在加载网页时使用 PHP 提供 xlsx 文件:

<?php
$fileName = 'Products.xlsx';
$filePath = "[PATH]/products.xlsx";
header('Content-disposition: attachment; filename=' . $fileName);
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: ' . filesize($filePath));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile($filePath);

将 [PATH] 替换为您的文件路径。

【讨论】:

  • 使用Content-Length: 标头中的filesize(...)非常小心;有很多原因这可能是不正确的,并且可能导致处理程序(Apache、nginx 等)仅将文件的一部分作为整个文件传递。我发现最好不要使用此标头,让浏览器自行解决。
猜你喜欢
  • 1970-01-01
  • 2012-10-05
  • 2011-01-09
  • 1970-01-01
  • 1970-01-01
  • 2014-12-29
  • 2013-12-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多