如何在不使用查询字符串的情况下找到目标页面中已单击哪个锚点进行下载
我最初有一个网络表单,其 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你需要 JavaScript。首先,在选择页面上,向每个
添加一个 id 属性,其值将告诉您要下载哪个文件。然后使用 JavaScript(jQuery 很容易做到这一点)来劫持锚标记:
preventDefault() on an preventDefault() a> tag
劫持是每个锚标记上的 onclick 事件,告诉客户端不要跟随 href。相反,它读取锚点的 id 和 href 值,并且:
接收页面读取 cookie 或表单字段分别确定要下载的文件。
更新
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:
The receiving page reads the cookie or form field, respectively, to determine what file to serve for download.
Update
我认为如果不使用查询字符串,这是不可能的,或者至少是合理可行的。至少,它可能会非常依赖于浏览器。所讨论的表单元素不会主动在“表单”上“执行”任何操作,它们只是浏览器呈现的链接。单击它们只是通知浏览器应加载
href
中的 URL。因此,您需要的所有信息都必须位于该 URL 中。为什么厌恶使用查询字符串参数?这正是他们的目的,将行为标志传递给动态资源(页面)。
编辑:一种可能符合您要求的想法是用 LinkButton 服务器控件替换这些 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.