如何使用Grails重定向文件下载?

时间:2023-01-27 11:35:19

I'm currently writing a website that allows people to download Excel and text files. Is there a way to redirect to a different page when they click, so that we run javascript and do analytics (i.e. keep download count)? Currently, nothing prevents the user from simply right-clicking and saving.

我目前正在编写一个允许人们下载Excel和文本文件的网站。有没有办法在点击时重定向到另一个页面,以便我们运行javascript并进行分析(即保持下载次数)?目前,没有什么能阻止用户简单地右键单击并保存。

Edit:

To be more specific, it would be nice for a single or double click of a file link to redirect to a temporary download page for analytics, then have the file be downloaded.

更具体地说,单击或双击文件链接以重定向到临时下载页面进行分析会很好,然后下载文件。

2 个解决方案

#1


2  

I started describing how you might do this in Grails but then remembered most analytics services (Google, Omniture, etc.) will let you track downloaded files by using the onclick event. If you have some custom javascript based tracking you're doing, you can do the same thing. The onclick will get called before the document starts downloading. For example:

我开始描述如何在Grails中执行此操作,但后来记住大多数分析服务(Google,Omniture等)将允许您使用onclick事件跟踪下载的文件。如果你有一些基于javascript的自定义跟踪,你可以做同样的事情。在文档开始下载之前将调用onclick。例如:

<a href="/path-to-download-file" onclick="record_download('filename')">myfile.txt</a>

More specifically for Google Analytics, here's some javascript to do this automatically: http://www.goodwebpractices.com/downloads/gatag.js

更具体地说,对于谷歌分析,这里有一些javascript自动执行此操作:http://www.goodwebpractices.com/downloads/gatag.js

#2


0  

I'm not sure what you are asking here, are you trying to figure out how to redirect in the controller or are you trying to override the right-click behavior in the browser?

我不确定你在这里问的是什么,你是想弄清楚如何在控制器中重定向,还是试图覆盖浏览器中的右键单击行为?

To redirect in the controller you can do something like this documented here.

要在控制器中重定向,您可以执行此处记录的此类操作。

redirect(controller:"book",action:"list")

If you are trying to change button or link behavior that's client side and will require some Javascript most likely.

如果您尝试更改客户端的按钮或链接行为,并且最有可能需要一些Javascript。

If you clarify I might be able to help more.

如果你澄清我可能会提供更多帮助。

#1


2  

I started describing how you might do this in Grails but then remembered most analytics services (Google, Omniture, etc.) will let you track downloaded files by using the onclick event. If you have some custom javascript based tracking you're doing, you can do the same thing. The onclick will get called before the document starts downloading. For example:

我开始描述如何在Grails中执行此操作,但后来记住大多数分析服务(Google,Omniture等)将允许您使用onclick事件跟踪下载的文件。如果你有一些基于javascript的自定义跟踪,你可以做同样的事情。在文档开始下载之前将调用onclick。例如:

<a href="/path-to-download-file" onclick="record_download('filename')">myfile.txt</a>

More specifically for Google Analytics, here's some javascript to do this automatically: http://www.goodwebpractices.com/downloads/gatag.js

更具体地说,对于谷歌分析,这里有一些javascript自动执行此操作:http://www.goodwebpractices.com/downloads/gatag.js

#2


0  

I'm not sure what you are asking here, are you trying to figure out how to redirect in the controller or are you trying to override the right-click behavior in the browser?

我不确定你在这里问的是什么,你是想弄清楚如何在控制器中重定向,还是试图覆盖浏览器中的右键单击行为?

To redirect in the controller you can do something like this documented here.

要在控制器中重定向,您可以执行此处记录的此类操作。

redirect(controller:"book",action:"list")

If you are trying to change button or link behavior that's client side and will require some Javascript most likely.

如果您尝试更改客户端的按钮或链接行为,并且最有可能需要一些Javascript。

If you clarify I might be able to help more.

如果你澄清我可能会提供更多帮助。