I am trying to use the following code for a project at work that Berico posted in viewtopic.php?t=8591.
The code is:
Code: Select all
; HTTP file download
;
If InitNetwork() = 0
MessageRequester("Error", "Can't initialize the network !", 0)
End
EndIf
#bufferlengte=10240
Global buffer
buffer = AllocateMemory(#1,#bufferlengte , 0)
Global buf$
Global EOL$
EOL$ = Chr(13)+Chr(10)
Global filenaam$
filenaam$="PureBasic_Update_3.81_Beta_1.zip"
server$="www.purebasic.com"
url$="http://www.purebasic.com/"
;http://www.purebasic.com/PureBasic_Update_3.81_Beta_1.zip
;
Port = 80
Global size
Global oldsize
Global rate
Global ratetel
Global buf$
Global header
Global startReceive
Global hwnd
Global ConnectionID
Global filesize
Global timeout
Global aniwin
Global h
Global einde
h=LoadLibrary_("Shell32.dll")
;
Procedure incoming(result)
b.b=0
b$=""
rest=0
Select header
Case 0
sendmessage_(AniWin,#ACM_OPEN,h,160)
i=0
Repeat
If PeekB(buffer+i)=13 And PeekB(buffer+i+1)=10 And PeekB(buffer+i+2)=13 And PeekB(buffer+i+3)=10
b$=Space(i+4)
CopyMemory(buffer,@b$,i+4)
rest=i
i=result-1
EndIf
i+1
Until i=result
;
Repeat
;
If Left(b$,2)=EOL$
If startreceive=1
b$=Mid(b$,3,Len(b$)-3)
If CreateFile(1,filenaam$)
WriteData(buffer+rest+4, result-rest-4)
header=1
size=result-rest-4
SetGadgetText(4,"Received "+Str(size)+" of "+Str(filesize)+" bytes")
Else
header=2
EndIf
EndIf
Else
search=FindString(b$, EOL$ , 1)
If search>0
l$=Left(b$,search-1)
b$=Mid(b$,search+2,Len(b$))
pos=FindString(l$,"200 " , 1)
If pos
startreceive=1 ; ok
Else
pos=FindString(l$,"404 " , 1)
If pos
;error 404 not founf
Else
pos=FindString(l$,"Content-Length:" , 1)
If pos
pos=FindString(l$," " , 1)
filesize=Val(Mid(l$,pos+1,Len(l$)))
EndIf
EndIf
EndIf
Debug l$
Else
l$=""
EndIf
EndIf ;
Until search=0
Case 1
timeout=0
WriteData(buffer, result)
size+result
SetGadgetText(4,"Received "+Str(size)+" of "+Str(filesize)+" bytes")
stap=100*size/filesize
SetGadgetState(2, stap)
If filesize=size
Debug "File Received"
header=2
Debug Str(size)+" of "+Str(filesize)+" bytes"
killTimer_(hWnd,1) ; 20 milisecond timer
killTimer_(hWnd,2) ; 1 sec timer
killtimer_(hWnd,3) ; 500 msec timer
CloseFile(1)
CloseNetworkConnection(ConnectionID)
Debug "Connection Closed"
DestroyWindow_(AniWin)
einde=1
EndIf
EndSelect
EndProcedure
;
Procedure MyWindowCallback(WindowID, Message, wParam, lParam)
Result = #PB_ProcessPureBasicEvents
Select WindowID
Case hwnd
Select message
Case #WM_TIMER
result=1
Select wparam
Case 1 ; timer
If NetworkClientEvent(ConnectionID)
result=ReceiveNetworkData(ConnectionID, buffer, #bufferlengte)
incoming(result)
EndIf
Case 2 ; timeout
timeout+1
If timeout>10
CloseFile(1)
header=2
Debug "Timeout"
EndIf
Case 3
ratetel+500
If ratetel>0
rate=size/ratetel
EndIf
SetGadgetText(3,"Download speed..."+Str(rate)+" KB/s. Time "+Str(ratetel/1000)+" s.")
EndSelect
EndSelect
EndSelect
ProcedureReturn Result
EndProcedure
;
hwnd=OpenWindow(0,100,450,335,160,#PB_Window_SystemMenu,"Downloading "+filenaam$+" from "+server$)
;
If hwnd
If CreateGadgetList(WindowID())
ButtonGadget(1, 250, 128, 72, 20, "Cancel")
ProgressBarGadget(2, 10, 100, 313, 20, 0, 100)
TextGadget(3, 20, 80, 280, 15, "")
TextGadget(4, 20, 65, 280, 15, "")
;
AniWin=CreateWindowEx_(0,"SysAnimate32","",#ACS_AUTOPLAY|#ACS_CENTER|#ACS_TRANSPARENT|#WS_CHILD|#WS_VISIBLE|#WS_CLIPCHILDREN|#WS_CLIPSIBLINGS,25,10,280,50, hwnd,0,GetModuleHandle_(0),0)
;
ConnectionID = OpenNetworkConnection(server$, Port)
;
If ConnectionID
SendNetworkString(ConnectionID, "GET "+URL$+filenaam$+" HTTP/1.0"+eol$)
SendNetworkString(ConnectionID, eol$)
;
SetWindowCallback(@MyWindowCallback())
; -------------- timers ----------------
SetTimer_(hWnd,1,1,0) ; 1 milisecond timer
SetTimer_(hWnd,2,1000,0) ; 1 sec timer
SetTimer_(hWnd,3,500,0) ; 500 msec timer
;
Repeat
EventID=WaitWindowEvent()
If EventID = #PB_EventGadget
Select EventGadgetID()
Case 1
header=2
EndSelect
EndIf
Until einde=1
EndIf
EndIf
EndIf
FreeLibrary_(h)
Delay(4000)
;
End
Line 10: A Constant Name need to start with a character (a-z or _)
The line it fails on is: buffer = AllocateMemory(#1,#bufferlengte , 0)
Any Ideas how to fix this? Thanks in advance