【问题标题】:I don't want the user to get the file directly from the url我不希望用户直接从 url 获取文件
【发布时间】:2021-11-27 09:10:07
【问题描述】:

例如:

url(`https://example.com/file.exe`)

如果用户直接访问本网站,用户将被重定向到https://example.com/getfile.html 但是如果用户从https://example.com/getfile.html<a>标签获取文件,则允许用户获取文件

【问题讨论】:

  • 我们不知道您的后端语言是什么。使用 PHP,您可以在 GET 调用和 POST 调用之间产生差异。

标签: javascript html server


【解决方案1】:

您可以通过删除锚标签中的 href 属性并将 onclick 功能添加到锚标签来实现此目的。在 onclick 函数中,当用户访问页面时,在 url 中发送一些参数以在您的 html 页面中获取该值。

Click here知道如何在url中发送参数。

Click here知道如何从url获取post参数。

您仍有任何困惑,您可以在此答案中评论您的问题。

如果您使用的是 php,您可以使用 $_REQUEST["your post parameter name"]; 来在一行代码中从 url 获取 post 参数;

我正在为此添加一个简单的示例,以使用简单的 html 和 jquery 来实现。

使用 jquery document.referrer 方法在文档准备好(当用户打开页面时),您可以获取用户之前访问的 url。像下面的例子。为此,您需要通过在页面中插入以下行来在页面中包含 jquery。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
  console.log(document.referrer);
if(document.referrer == "your url") {
    your code to allow user if comes from the url you mentioned //user will get download button or you can write direct download option;
} else {
     window.location = "some url";   //you can redirect user to specific url
}
});
<script>

【讨论】:

  • 你在后端使用的技术是什么,除了html、javascript
  • 我已经用 jquery 代码更新了答案。请检查一次
  • 是的,我知道,但我认为这只能在 html 中使用,但我希望它影响我的 file.exe
  • 您只能使用 jquery 来实现这一点。使用 html,您无法获取用户以前的 url,或者如果他直接来了
  • 我不知道我可以在文件中使用什么语言来做到这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多