Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using xmlhttp object to download file

Status
Not open for further replies.

pilg

Programmer
Feb 16, 2001
82
GB

I understand that the XMLHTTP object is cabable of downloading files from a remote location.

Does anyone know how this is done? I have tried google with no luck.

I need to download a .CSV file and then run some processing on it.
 
i have done this similar thing in the past using winhttp 5.1

do you need code for that...

-DNG
 
sorry i meant to say using xmlhttp...

Code:
dim objXMLHTTP,yourfile

yourfile="myfile.csv"
DestFolder = "./"

URL = "your URL here/" &yourfile

set objXMLHTTP = CreateObject("Msxml2.ServerXMLHTTP.4.0")


objXMLHTTP.Open "GET", URL, False
objXMLHTTP.Send 


set oStream = createobject("Adodb.Stream")
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
Const adSaveCreateNotExist = 1

oStream.type = adTypeBinary
oStream.open
oStream.write objXMLHTTP.responseBody


oStream.savetofile DestFolder & yourfile, adSaveCreateNotExist

oStream.close

set oStream = nothing
Set objXMLHTTP = Nothing


-DNG
 
Thank you very much for the code! I haven't been able to set it up yet but I'll update this forum with my results.
 
Hi,

I recieve the following error when I run the code:

Microsoft VBScript runtime error '800a01ad'

ActiveX component can't create object: 'Msxml2.ServerXMLHTTP.4.0'

/FileDownloadTest.asp, line 18

Do I need to install ASP.NET on the server?

 
no...you dont have to install asp.net...you just need to install XMLHTTP.4.0 component...


-DNG
 
Sorry but how do I do I install the component?
 
Ok seems to be doing something now but it does not seem to download the file.

I don't quite understand what the code is doing. The URL I have links directly to a downloadable .cvs file. is this what the code is expecting?

if i put the url into a browser I get the option to open or save the file does the code deal with this?

Sorry if I am sounding completely dumb but I am totally new to this.

Any advice will be well appreciated.
 
ok...here is what we are doing:

Code:
[red]'declaring our variables[/red]
dim objXMLHTTP,yourfile
[red]setting variable to your file name[/red]
yourfile="myfile.csv"
[red]set where you want to store the file[/red]
DestFolder = "./"

[red]this is how your URL should look
[URL unfurl="true"]http://mysitename/myfile.csv[/URL][/red]
URL = "your URL here/" &yourfile

[red]setting xmlhttp object[/red]
set objXMLHTTP = CreateObject("Msxml2.ServerXMLHTTP.4.0")

[red]getting the above url[/red]
objXMLHTTP.Open "GET", URL, False
[red]sending the request[/red]
objXMLHTTP.Send 

[red]reading the data stream[/red]
set oStream = createobject("Adodb.Stream")
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
Const adSaveCreateNotExist = 1

oStream.type = adTypeBinary
oStream.open
oStream.write objXMLHTTP.responseBody

[red]saving it to a file on destination folder with
the same name[/red]
oStream.savetofile DestFolder & yourfile, adSaveCreateNotExist
[red]close and dispose everything[/red]
oStream.close

set oStream = nothing
Set objXMLHTTP = Nothing
isnt that simple :)

-DNG
 
ok then try this code...

Code:
dim objXMLHTTP,yourfile

DestFolder = "./"

URL = "[URL unfurl="true"]http://directory.intra.bt.com/ids/owa/people.csv?planner=Y&;abook_bid=PILGRIL2&ab_num=0&wa_start_date=29-AUG-05"[/URL]

set objXMLHTTP = CreateObject("Msxml2.ServerXMLHTTP.4.0")


objXMLHTTP.Open "GET", URL, False
objXMLHTTP.Send 


set oStream = createobject("Adodb.Stream")
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
Const adSaveCreateNotExist = 1

oStream.type = adTypeBinary
oStream.open
oStream.write objXMLHTTP.responseBody

myfilename="test.csv"
oStream.savetofile DestFolder & myfilename, adSaveCreateNotExist

oStream.close

set oStream = nothing
Set objXMLHTTP = Nothing

-DNG
 
Thank you very much for pursueing with this!!

The script runs with no errors unless I hit refresh and it complains that it cannot write to the file. Although the file does not appear in the directory.

Like I said when I run the URL it attempts to download a file called people.csv. do we need to name the file in the script? would this cause it not to work?
 
make sure you have the write permissions on the folder...

and in the code when we did this...

oStream.savetofile DestFolder & myfilename, adSaveCreateNotExist

it should save it to the current folder with file name test.csv

i am sure the problem here is with the write permissions...

-DNG
 
Unless you have changed your IIS web server's default permissions then the above page will run in the security context of the IUSR_MachineName account.

Check the destination folder and make sure this account has write access.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top