kmdxb

Trying to read a binary file from a remote web site, it seems that responseStream is not part of the XMLHttpRequest that is available from withing the browser.

If I create my xhr object as

xhr=new ActiveXObject("MSXML2.XMLHTTP.3.0");

the I have xhr.responseStream available, but if created as

xhr=new XMLHttpRequest()

then it is not available.

OK, that's not too difficult to work around (just use the ActiveX version), but it would of been nice for responseStream to be present in XMLHttpRequest as well.

Now, on to the actual problem... Once I have downloaded a binary object and have the responseStream data, how can I write this to a file. The Scripting.FileSystemObject only seems to be for text data, not binary.

Does anybody know of a method to write a binary file

Regards,

Keith.



Re: Sidebar Gadget Development XMLHttpRequest responseStream

Andy E

I think you can do it using an ADO Binary Stream, but I'm not 100%. Jonathan Abbott might know or you could search google for ADODB.Stream

Andy




Re: Sidebar Gadget Development XMLHttpRequest responseStream

kmdxb

Seems that I can use XMLHttpRequest - just needed to put responseBody instead of responseStream.

Using ADODB.Stream is working for saving the data. An example of doing this (as part of the XMLHttpRequest handler, where you have already checked it is a 200 OK response etc)

Code Snippet

var adTypeBinary = 1;
var adTypeText = 2;
var adSaveCreateOverWrite = 2;

var stream = new ActiveXObject("ADODB.Stream");
stream.type = adTypeBinary
stream.open();
stream.write(xhr.responseBody);
stream.saveToFile(download_target, adSaveCreateOverWrite);
stream.close()

xhr is your XMLHttpRequest object, and download_target is the full filename of where you want to store the data (e.g. download_target=System.Gadget.path+"\\tempfile.dat" )

Just need to add something now to see if a file already exists (no need to download it if it does)





Re: Sidebar Gadget Development XMLHttpRequest responseStream

kmdxb

Checking if a file exists trurns out to be easy, just use the FileExists function from Scripting.FileSystemObject.