如何在不使用查询字符串的情况下找到目标页面中已单击哪个锚点进行下载

发布于 2024-10-09 16:40:05 字数 404 浏览 0 评论 0原文

我最初有一个网络表单,其 href 如下:

   <a href="downloadInfo.aspx">ACH File Management System Trail(msi)</a>
   <a href="downloadInfo.aspx">ACH File Management System Trail(zip)</a>

这是我的两个锚标记,当我单击此标记时,我会将它们重定向到同一页面,用户必须在其中填写详细信息,并且将向给定邮件的用户发送一封邮件ID。当用户单击邮件时,我希望下载他选择下载的内容。如果 msi 意味着我想提示下载 msi 文件,如果应该下载 zip 文件,

我需要使用查询字符串来解决这个问题

I am having a web form initially which have href as follows

   <a href="downloadInfo.aspx">ACH File Management System Trail(msi)</a>
   <a href="downloadInfo.aspx">ACH File Management System Trail(zip)</a>

These are my two anchor tags when i click on this i will redirect both to a same page where user has to fill details and a mail will be send to the user for the given mail id. When the user clicks on mail i would like to have the download for which he opted to download. If msi means i would like to prompt msi file to be downloaded and if zip it should be downloaded

I need this to be worked with out using query-string

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

苍景流年 2024-10-16 16:40:05

你需要 JavaScript。首先,在选择页面上,向每个 添加一个 id 属性,其值将告诉您要下载哪个文件。然后使用 JavaScript(jQuery 很容易做到这一点)来劫持锚标记:

preventDefault() on an preventDefault() a> tag

劫持是每个锚标记上的 onclick 事件,告诉客户端不要跟随 href。相反,它读取锚点的 id 和 href 值,并且:

  1. 将 id 设置为 cookie,然后将 window.location 设置为锚点的 href 或
  2. 将 id 值作为表单字段发布到锚点的 href

接收页面读取 cookie 或表单字段分别确定要下载的文件。

更新

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-1.4.4.js"></script>
</head>
<body>
    <a id="ach-msi" href="downloadInfo.aspx">ACH File Management System Trail(msi)</a><br/>
    <a id="ach-zip" href="downloadInfo.aspx">ACH File Management System Trail(zip)</a>
    <script>
    $("a").click(function(event) {
        event.preventDefault();
        var id = $(this).attr('id');
        var href = $(this).attr('href');
        /* this uses the cookie option */
        document.cookie = 'download-file=' + id;
        window.location = href;
    });
    </script>
</body>
</html>

You'll need JavaScript. First, on the choose page, add an id attribute to each <a> whose value would tell you what file to download. Then use JavaScript (jQuery does this easily) to hijack the anchor tags:

preventDefault() on an <a> tag

The hijack is an onclick event on each anchor tag that tells the client to not following the href. Instead it reads the anchor's id and href values and either:

  1. sets the id as a cookie and then sets the window.location to the anchror's href OR
  2. posts the id value as a form field to the anchor's href

The receiving page reads the cookie or form field, respectively, to determine what file to serve for download.

Update

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-1.4.4.js"></script>
</head>
<body>
    <a id="ach-msi" href="downloadInfo.aspx">ACH File Management System Trail(msi)</a><br/>
    <a id="ach-zip" href="downloadInfo.aspx">ACH File Management System Trail(zip)</a>
    <script>
    $("a").click(function(event) {
        event.preventDefault();
        var id = $(this).attr('id');
        var href = $(this).attr('href');
        /* this uses the cookie option */
        document.cookie = 'download-file=' + id;
        window.location = href;
    });
    </script>
</body>
</html>
懷念過去 2024-10-16 16:40:05

我认为如果不使用查询字符串,这是不可能的,或者至少是合理可行的。至少,它可能会非常依赖于浏览器。所讨论的表单元素不会主动在“表单”上“执行”任何操作,它们只是浏览器呈现的链接。单击它们只是通知浏览器应加载 href 中的 URL。因此,您需要的所有信息都必须位于该 URL 中。

为什么厌恶使用查询字符串参数?这正是他们的目的,将行为标志传递给动态资源(页面)。

编辑:一种可能符合您要求的想法是用 LinkBut​​ton 服务器控件替换这些 HTML 锚点。然后,每个事件都可以在回发上拥有自己的事件处理程序,该事件处理程序可以在服务器端维护状态(单击了哪一个),并执行 Response.Redirect(甚至可能是 Server.Transfer)到所需的目的地。

相反,您可以编写一个 JavaScript 函数,该函数使用包含单击的表单值向目标页面执行 POST,并在这些锚标记的 onClick 事件中调用该函数(向其传递值)。然后目标页面只需要读取 POST 值。

但这些比仅使用查询字符串值要迟钝得多。

I don't think this is going to be possible, or at least reasonably feasible, without making use of the query string. At the very least, it's likely going to be very browser-dependant. The form elements in question aren't actively "doing" anything on the "form", they're just links rendered by the browser. Clicking on them does nothing more than inform the browser that it should load the URL in the href. So all of the information you need is going to have to be in that URL.

Why the aversion to using query string parameters? This is precisely their purpose, passing a behavior flag to a dynamic resource (page).

Edit: One idea that may work within your requirements it to replace these HTML anchors with LinkButton server controls. Then each can have its own event handler on the postback which can maintain state (which one was clicked) server-side and do a Response.Redirect (or perhaps even Server.Transfer) to the desired destination.

Conversely, you could write a JavaScript function that performs a POST to the destination page with a form value containing which one was clicked and call that function (passing it the value) in the onClick event for those anchor tags. Then the destination page will just need to read the POST value.

But these are considerably more obtuse than just using a query string value.

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