WinBatch Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: bottomleypotts on May 03, 2018, 12:11:14 pm

Title: Problems downloading a PDF
Post by: bottomleypotts on May 03, 2018, 12:11:14 pm
I am having difficulty downloading a PDF. I can only seem to download the first couple of bytes of a 1Mb file. Can anyone tell me what I am doing wrong here? I have loaded fiddler and can see that the entire PDF is retrieved by the request.

Code: Winbatch
o=CreateObject(`MSXML2.XMLHTTP.6.0`)

url=`http://kmmc.in/wp-content/uploads/2014/01/lesson2.pdf`

o.Open(`GET`,url,@FALSE)
o.SetRequestHeader(`User-agent`,`Mozilla/5.0 (Windows NT 10.0; rv:55.0) Gecko/20100101 Firefox/55.0`)
o.SetRequestHeader(`Accept`,`text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8`)

o.Send()
While o.ReadyState!=4
        o.WaitForResponse(1000)
EndWhile

Rc=FilePut(DirScript():`lesson2.pdf`,o.ResponseText)
Title: Re: Problems downloading a PDF
Post by: td on May 03, 2018, 01:38:57 pm
A pdf document is binary and not text so you need to use a different object method (ResponseBody) and a different technique for writing it to a file (binary buffers).

Code: Winbatch
hBuf = BinaryAllocArray(o.ResponseBody)
BinaryWrite(hBuf, 'c:\temp\dump.pdf')
BinaryFree(hBuf)
 
Title: Re: Problems downloading a PDF
Post by: bottomleypotts on May 03, 2018, 08:16:12 pm
Thank you!