/    Sign up×
Community /Pin to ProfileBookmark

Downloading Files using javascript?

Im trying to download a file off of internet, off of a link, and saving it to my local drive using javascript. I searched for it online but no luck.. I’m wondering if you could even do that with javascript because im sure you need an object to do that, it’s a not scripting but if anyone knows how to do it, please post the codes or give a little notes on how to do it.

Any help is apperciated,

Sam

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@nobletimeauthorDec 30.2008 — just to back up on what I just said.

say the link is : http://whatever.com/file.zip

I want the script to automatically start process of downloading file.zip and save it on forinstance C:downloads

using a function in javascript that can take a link and destination on local drive where you want it to be saved.

Thanks,

Sam
Copy linkTweet thisAlerts:
@nobletimeauthorJan 01.2009 — no reply yet. so i guess it cant be done.
Copy linkTweet thisAlerts:
@felgallJan 01.2009 — The closest JavaScript can get to downloading a zip file would be to do a [B]location.href='file.zip';[/B] that would attempt to load the zip file into the browser and which would then pop up a dialog asking where to save the file or a [B]window.open('file.zip','_blank');[/B] which would open a new window so that the current web page isn't overwritten by the file download.

Neither way do you get to specify where to save the file since you don't know what file structure the operating system your visitor is using and so have no idea where to save to (most operating systems don't use C:/ as a file location).

There are ways to save files using JScript (a slightly different language) but that would only work on Internet Explorer running on an intranet where you have access to configure all the browsers.
Copy linkTweet thisAlerts:
@voidvectorJan 01.2009 — I assume you want it done in WSH-based JScript, because you don't have direct access to the local file system from browser-based JavaScript.

Here is a VBScript implementation I made awhile ago. If you understand both JScript and VBS it should be easy to convert it into JScript.
<i>
</i>Sub DownloadFile(sURL, sFilename)
Dim oXMLHTTP, oStream

<i> </i>On Error Resume Next
<i> </i>Set oStream = CreateObject("ADODB.Stream")

<i> </i>' Make sure object was created
<i> </i>If (Not IsObject(oStream)) Then
<i> </i> Run sURL
<i> </i> Exit Sub
<i> </i>End If
<i> </i>On Error GoTo 0

<i> </i>' Fetch the file
<i> </i>Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP")

<i> </i>oXMLHTTP.open "GET", sURL, false
<i> </i>oXMLHTTP.send()

<i> </i>If oXMLHTTP.Status = 200 Then
<i> </i> oStream.Open
<i> </i> oStream.Type = 1 'adTypeBinary

<i> </i> oStream.Write oXMLHTTP.ResponseBody

<i> </i> oStream.SaveToFile sFilename, 2 ' adSaveCreateNotExist AND adSaveCreateOverWrite
<i> </i> oStream.Close
<i> </i> Set oStream = Nothing
<i> </i>Else
<i> </i> WScript.Quit()
<i> </i>End if

<i> </i>Set oXMLHTTP = Nothing
End Sub
Copy linkTweet thisAlerts:
@MrNobodyJan 01.2009 — Im trying to download a file off of internet, off of a link, and saving it to my local drive using javascript.[/QUOTE]
Technically, before any accurate answers can be given, one must clarify a few things. Otherwise, everybody is making assumptions about what you need.

Is this a case of the file is on your own website? ...or on somebody else's website? If on your own website, do you realize that no JavaScript is actually needed? A standard link will allow a ZIP file to be downloaded. Of course, it gives a prompt box as mentioned by felgal. Any other environmental considerations we should know about?
Copy linkTweet thisAlerts:
@nobletimeauthorJan 02.2009 — Thanks everyone for their Reply.

voidVector, good post! I'm going to try the code you posted, you made me realize that perhaps I need to learn JBScript to handle this file downloading. Thanks again!
Copy linkTweet thisAlerts:
@Totte_chJan 02.2009 — Many sites uses this to start a download ([FONT="Courier New"]<head>[/FONT] section):
[CODE][COLOR="Blue"]<meta [COLOR="Red"]http-equiv[/COLOR]=[COLOR="Magenta"]"Refresh"[/COLOR] [COLOR="red"]content[/COLOR]=[COLOR="magenta"]"0;URL=http://whatever.com/file.zip"[/COLOR]>[/COLOR][/CODE]
The number in the argument called [FONT="Courier New"]content[/FONT] sets the seconds to wait til the download starts.
Copy linkTweet thisAlerts:
@voidvectorJan 02.2009 — Thanks everyone for their Reply.

voidVector, good post! I'm going to try the code you posted, you made me realize that perhaps I need to learn JBScript to handle this file downloading. Thanks again![/QUOTE]


It is pretty easy to convert it to JScript.

ADODB.Stream was used so that the stream is treated as binary (instead of text as would be in a standard AJAX operation). I think there is a better XHR binary hack out there now, but I don't feel like doing the research and rewriting the code.

[code=php]

function DownloadFile(sURL, sFilename)
{
try
{
var oStream = new ActiveXObject('ADODB.Stream');
}
catch(e)
{
WScript.Echo('not supported');
WScript.Quit();
}

// Fetch the file
var oXMLHTTP = new ActiveXObject('MSXML2.XMLHTTP');

oXMLHTTP.open('GET', sURL, false);
oXMLHTTP.send();

if (oXMLHTTP.Status == 200)
{
oStream.Open();
oStream.Type = 1; //adTypeBinary

oStream.Write(oXMLHTTP.ResponseBody);

oStream.SaveToFile (sFilename, 2); // adSaveCreateNotExist AND adSaveCreateOverWrite
oStream.Close();
oStream = null;
}
else
{
WScript.Quit();
}

oXMLHTTP = null;
}

DownloadFile('http://www.irs.gov/pub/irs-pdf/fw4.pdf', 'test.pdf');

[/code]
×

Success!

Help @nobletime spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...