24Jan/0772
VBScript: Download and Save a Binary File
Update: If you are an iTunes user that needs VBScript, this page won't help you. Please visit the following two sites for two different solutions: Apple Docs and KeathMilligan.net.
This is an old script I dug up which I call "fileFetch". It's a script that downloads a binary file using XMLHTTP and saves it using an ADO stream. This is handy for nightly downloads and will work within SQL Server DTS packages.
' Set your settings
strFileURL = "http://www.domain.com/file.zip"
strHDLocation = "D:\file.zip"
' Fetch the file
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
objXMLHTTP.open "GET", strFileURL, false
objXMLHTTP.send()
If objXMLHTTP.Status = 200 Then
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1 'adTypeBinary
objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0 'Set the stream position to the start
Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
Set objFSO = Nothing
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
End if
Set objXMLHTTP = Nothing



October 20th, 2009 - 12:37
Malo, how can you tweak this code to download a file from a SharePoint Document Library? Do you have an example you can post here? Thanks!
October 21st, 2009 - 11:22
Never mind, Malo… I just discovered that all of my frustration has been due to the SharePoint server blocking MDB files for security reasons. GRRRR!!!! I renamed the file to a TXT file instead of an MDB file and uploaded the TXT version to sharepoint. The script worked just fine as is. Thanks for the script!
October 28th, 2009 - 02:00
is it feasible to add an authentication part into the script if a username & pwd is required?!?
thanks!
November 2nd, 2009 - 09:20
I am developing a website and need to allow some files to be download by viewers of the website. These files are .XLS (Excel), PDF and ZIP (combinations of XLS and PDF). Iwould like to be able to download them without the usual “Open”, “Save” or “Cancel” dialog.
I also need to work arount the Server 2003 4mB limit problem.
Do you have something or would you be willing to develope something for a fee? If so what would you charge?
November 12th, 2009 - 14:20
Thanx Chrissy!! Saved me som troublish time… You’re the best!!
November 12th, 2009 - 14:25
I will dedicate this function to you!
Public Function ChrissyFileFetch(strFileURL, strHDLocation)
[...your code...]
End Function
See U!
November 12th, 2009 - 18:14
haha thanks, Fredrik!
November 12th, 2009 - 18:15
Hey Earl,
Sorry, my plate is full currently. :\
Chrissy
November 13th, 2009 - 12:05
Great Script. My issue is that I do not have just one file such as file.zip to down load, I need to download several thousand image files on the fly. I have the URL paths and photo names to each in my database already so I need to dynamically populate the script usign T-Sql. I figure I need to use the FOR EACH…NEXT but I am not getting this done correclty
November 13th, 2009 - 15:54
actaully I figured it out.
November 26th, 2009 - 00:36
Hello,
I am trying to download a word document from sharepoint using the URL, but a corrupted file is being downloaded. I have used the same code as given above. Please anyone help me how to download file from sharepoint.
December 3rd, 2009 - 14:05
Nice script, Chrissy. Helped solve some problems. Thanks!
Seb, I added/changed Chrissy’s script to do http Basic Authentication like this.
key = base64_encode( username & “:” & password )
xmlhttp.open “GET”, url, False, username, password
xmlhttp.setRequestHeader “Authorization”, “Basic ” & key
xmlhttp.send
where base64_encode is a sub to do base64 encoding.
December 8th, 2009 - 05:02
Hi All,
Realy great script, but my question is “How to download files (xls) from web link that require SSO authentication”. Please help me out.
I tried using the code above, and passed sso id and password, but its downloaded the Login page in excel.
Please provide some hint, thanks in advance.
Thanks & Regards
Amit
December 9th, 2009 - 06:59
wow, you rule!
January 18th, 2010 - 12:30
How to rewrite a file in the destination?
February 9th, 2010 - 00:57
Excellent Script……I am able use it to automate downloading list of PDF files…..from webpages…
February 10th, 2010 - 16:19
THANKS A BUNCH CHRISSY.
THIS IS VERY VERY SIMPLE SCRIPT AND WORKS GREAT.
YOU ROCK!!!
February 24th, 2010 - 01:26
Is there a way to download a file in bits and pieces ? I have a file 0.5GB and it continue getting out of memory errors. Is there a way to page files or so ?
April 29th, 2010 - 10:51
i have lost my vb scrpit of my computer x
June 1st, 2010 - 13:41
Thanks a bunch I will try it out!
June 3rd, 2010 - 06:32
Thank you Chrissy..
It helped me a lot..
Keep doing the gud job…
August 9th, 2010 - 09:56
Thanks Chrissy and Tom!
This really helped me too!