javascript 下载链接如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4864129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:27:55  来源:igfitidea点击:

How does a javascript download link work?

javascripthtmldownload

提问by Exitos

I've been using the Microsoft Technet site and you can download the ISO files by clicking a link on the page. The element is like this:

我一直在使用 Microsoft Technet 站点,您可以通过单击页面上的链接来下载 ISO 文件。元素是这样的:

<a href="javascript:void(0)" onmouseout="HideToolTip()"
    onmouseover="ShowToolTip(event,'Click here to download.')"
    onclick="javascript:RunDownload('39010^313^164',event)"
    class="detailsLink">Download</a>

I wasn't able to find the RunDownload()method in the scripts. And I wondered what it is likely to do. I mean usually when I provide a link for someone to download I provide an anchor to it:

我无法RunDownload()在脚本中找到该方法。我想知道它可能会做什么。我的意思是通常当我提供一个链接供某人下载时,我会提供一个锚点:

<a href="www.foo.com/mymp3.mp3">download</a>

But this is working differently what is the script doing? Because even when I ran 'Fiddler' I wasn't able to see the actual download location.

但这工作方式不同,脚本在做什么?因为即使我运行“Fiddler”,我也无法看到实际的下载位置。

回答by BiAiB

there's no such thing as a "javascript download" link. Javascript can open a new window, or simulate a click on a link.

没有“javascript下载”链接之类的东西。Javascript 可以打开一个新窗口,或模拟点击链接。

What you have to find is which url the function triggered by this click will lead to.

你需要找到的是这个点击触发的函数会导致哪个url。

here's an example of how to do it:

这是如何做到这一点的示例:

Suppose we have a:

假设我们有一个:

<a id="download">download Here §§§</a>

then this jQuery code:

那么这个jQuery代码:

$('#download').click( function() {
    window.location.href = 'http://example.org/download/ISO.ISO';
} );

will redirect to the URL http://example.org/download/ISO.ISO. Whether this url starts a download or not depends on HTTP headers and your browser, not on what javascript do.

将重定向到 URL http://example.org/download/ISO.ISO。这个 url 是否开始下载取决于 HTTP 标头和您的浏览器,而不是 javascript 做什么。

回答by Matías Fidemraizer

Download location can be a url-rewritten path. This mean that maybe some parameters are given with HTTP Post and some HTTP handler in the Web server or web application may be getting some arguments from the HTTP request and write file bytes to an HTTP response, which absolutely hides where the file is located in the actual server's file system.

下载位置可以是 url 重写的路径。这意味着可能一些参数是通过 HTTP Post 给出的,而 Web 服务器或 Web 应用程序中的一些 HTTP 处理程序可能会从 HTTP 请求中获取一些参数并将文件字节写入 HTTP 响应,这绝对隐藏了文件在实际服务器的文件系统。

Maybe this is what's behind the scenes and prevents you to know the file location.

也许这就是幕后的事情,并阻止您知道文件位置。

For example, we can have this: http://mypage.com/downloads/1223893893

例如,我们可以这样:http: //mypage.com/downloads/1223893893

And you requested an executable like "whatever.exe" for downloading it to your hard disk. Where's the "http:/mypage.com/downloads/whatever.exe"? Actually, it doesn't exist. It's a byte array saved in a long database in some record, and "mypage" web application handles a request for a file that's identified as "1223893893" which can be a combination of an identifier, date time or whichever argument.

并且您请求了一个类似于“whatever.exe”的可执行文件以将其下载到您的硬盘上。“http://mypage.com/downloads/whatever.exe”在哪里?实际上,它不存在。它是一个字节数组,保存在某个记录的长数据库中,“mypage”Web 应用程序处理对标识为“1223893893”的文件的请求,该文件可以是标识符、日期时间或任何参数的组合。

回答by Waqas Tahir

What I think the function RunDownloadmight do is that it might inform the server using get request to the server that another download is about to happen , or it might need to run the download background by setting the target attribute to an iframe so the user won't need to open another tab and download the file on the same page.

我认为RunDownload函数可能会做的是它可能会使用 get 请求通知服务器另一次下载即将发生,或者它可能需要通过将目标属性设置为 iframe 来运行下载后台以便用户获胜不需要打开另一个选项卡并在同一页面上下载文件。

<a href="file.mp3" onclick="runDownload();">Download</a>

JS

JS

var runDownload=function(){
    e.preventDefault();
    increaseDownloadCountOnTheServer(location);
    window.location.href="filelocation.exe";
}