[Gambas-user] Using a socket to download an image. Please Help ^_^
Robert Moss
the.at.robert at ...626...
Sun Nov 30 11:06:25 CET 2008
I am trying to get an image (or any binary data) downloaded from a website
and into a jpg file. I expect I will need to use the Byte[] datatype, but I
absolutely can NOT use Callbacks when the data comes in this time.
Here is my example:
DIM RequestString as string
DIM sTotal as string
DIM sBuf as string
Sock.Connect("host",80)
RequestString = MakeGetRequest("/imagesurl.jpg") 'returns a string like
HTTP/1.1 GET /page... \r\n its all correct
WRITE #Sock, RequestString, Len(RequestString)
'Code to wait for connection, etc is removed for brevity
DO UNTIL(EOF(#Sock))
READ #Sock, sBuf, Lof(Sock)
sTotal &= sBuf
LOOP
CLOSE #Sock
And after I separate the Content of the reply and dump it to a file, its not
readable, I presume this is because I am trying to write binary data as a
string
p1 = InStr(sTotal, "\r\n\r\n")
ImgData = Mid(sTotal, p1)
OPEN "localimg.jpg" FOR OUTPUT CREATE AS #myFile
WRITE #myFile, ImgData, Len(ImgData)
CLOSE #myFile
Keep in mind, the http request looks something like
HTTP/1.0 200 OK
Header-X: Value1
Content-Type: image/jpeg
AFdka;h3ijaf #RVA 'Image Data
So I need a way to correctly write the binary data to a file without also
including the content headers
Thank you
More information about the User
mailing list