Exitos
Exitos

Reputation: 29720

How does a javascript download link work?

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:

<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:

<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.

Upvotes: 6

Views: 24471

Answers (3)

Waqas Tahir
Waqas Tahir

Reputation: 8242

What I think the function RunDownload might do is crate another link, fetching the link from the server, This is done so that a link is accessible for some period of time, So server is informed to create a unique link for the user, Which is valid for some period of time

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

JS

var runDownload=function(){
    e.preventDefault();
    let link = fetchLinkFromServer()
    let a = document.createElement("a")
    a.setAttribute("download", "")
    a.href = link
    a.click();
}

Upvotes: 0

BiAiB
BiAiB

Reputation: 14122

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

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

here's an example of how to do it:

Suppose we have a:

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

then this jQuery code:

$('#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.

Upvotes: 8

Mat&#237;as Fidemraizer
Mat&#237;as Fidemraizer

Reputation: 64923

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.

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

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.

Upvotes: 3

Related Questions