Problems downloading a PDF

Started by bottomleypotts, May 03, 2018, 12:11:14 PM

Previous topic - Next topic

bottomleypotts

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) Select
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)


td

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) Select
hBuf = BinaryAllocArray(o.ResponseBody)
BinaryWrite(hBuf, 'c:\temp\dump.pdf')
BinaryFree(hBuf)
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

bottomleypotts