0

I am attempting to download 'Open Document Format' files from a node.js application which is being converted/exported through Open Office on the server (HTML to various formats). This works great for PDF, Text, HTML, Word etc. but does not work with documents exported as ODF. Converting and downloading the same files directly through the web application front-end works fine; downloading through XMLHTTP and saving to disk using ADO corrupts the document for some reason.

Here is my function:

Public Function downloadExport(fileToDownload, saveToPath)

Dim xmlhttp, ostream As Object

Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
Set ostream = CreateObject("Adodb.Stream")

xmlhttp.setOption(2) = 13056
xmlhttp.open "GET", fileToDownload, False
'Cookie headers are correct
xmlhttp.setRequestHeader "Cookie", "sessionID=Arrays.4Qqu2s32xQQyZA4"
xmlhttp.setRequestHeader "Cookie", "express_sid=s%3ADHclQm7vYT1Ixa2SD2wjk"
xmlhttp.send

ostream.Type = 1
ostream.open
ostream.write xmlhttp.responseBody
ostream.SaveToFile saveToPath, 1
ostream.Close

Set ostream = Nothing
Set xmlhttp = Nothing

End Function

Thank you EOF

Mark Bertenshaw
  • 5,594
  • 2
  • 27
  • 40

1 Answers1

0

Skip the ADO Stream and just retrieve .responseBody into a dynamic Byte array. Then open a file for binary writing and write the Byte array. Just use normal VB6 I/O statements.

Also your data declarations are rough, and your xmlhttp is a Variant (and so the code works "just by luck").

Better yet set references to the libraries, use early binding, and replace magic numbers by predefined named constant values.

Your usage of .setOption looks incorrect as well, are you sure you don't blow up on an exception ("argument not optional") there? This is a Method, not a Property.

But I doubt any of that will solve the problem you are seeing.

Messing around with session cookies also suggests you are doing something improper (as in illicit) here. Consult that site's terms of use. Webscraping is piracy.

Bob77
  • 13,167
  • 1
  • 29
  • 37
  • Well it looks like my problem was much easier to resolve than I originally thought. A simple change from "ODF" to "ODT" file extension was all that was needed to fix it (the code I submitted seems to work okay). The .setOption method is used to prevent XMLHTTP from generating certificate authority errors and was set incorrectly. As for session cookies, that was just a temporary test string. I will eventually write a login function that will set session info to a global variable and send it to my server (the server that resides in my garage, nothing illicit going on here... ). Thanks. – AtLeast3Characters Apr 19 '14 at 01:10