Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.clipper.visual-objects > #7593
| From | JohnMartens <martens@tennishulp.info> |
|---|---|
| Newsgroups | comp.lang.clipper.visual-objects |
| Subject | Re: FTP in VO, any samples please? |
| Date | 2014-12-08 12:54 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <m643he$5h9$1@dont-email.me> (permalink) |
| References | <m642l8$2cp$1@dont-email.me> |
Hi Martin,
Something like the code below.
John
oFtpProgressWindow:Toelichting := 'Connectie opzetten'
oFtp := MC_Ftp{oFtpProgressWindow,'MTCS download'}
oFtp:ConnectRemote(SELF:oGlobOptie:cFtpUrl,SELF:oGlobOptie:cFtpUser,SELF:oGlobOptie:cFtpPasswd,!
SELF:oGlobOptie:lFtpPassiveMode)
DO CASE
CASE ! IsInternetConnected()
* Message
CASE ! oFtp:Connected
* Message
CASE Empty(SELF:oGlobOptie:cFtpDirectory)
CASE Empty(oFtp:SetCurdir(SELF:oGlobOptie:cFtpDirectory))
* Message
ENDCASE
oFtp:GetDirectory(cFileName)
DO CASE
CASE Empty(oFtp:Dir)
* Message
OTHERWISE
oFtpProgressWindow:Start()
IF ! oFtp:GetLowLevel(cFileName,cFileName,FALSE)
* Message
ENDIF
ENDCASE
oFtp:CloseRemote()
Here is the class MC_Ftp
CLASS MC_Ftp INHERIT cFTP
PROTECT _Owner AS OBJECT
PROTECT _Status AS STRING
PROTECT _Dir AS ARRAY
PROTECT _PreventShow AS LOGIC
PROTECT dwOldSecondsNeeded := 0 AS DWORD
ACCESS Dir CLASS MC_FTP
RETURN SELF:_Dir
METHOD GetAttrib(xFile) CLASS MC_Ftp
LOCAL nPos AS INT
LOCAL sRet AS STRING
DO CASE
CASE IsNumeric(xFile)
nPos:=xFile
OTHERWISE
nPos:=AScan(SELF:_Dir,{|aSub| aSub[F_NAME]==xFile} )
END
IF nPos>0
sRet:=SELF:_Dir[nPos, F_ATTR]
ENDIF
RETURN sRet
METHOD GetDate(xFile) CLASS MC_Ftp
LOCAL nPos AS INT
LOCAL dRet AS DATE
DO CASE
CASE IsNumeric(xFile)
nPos:=xFile
OTHERWISE
nPos:=AScan(SELF:_Dir,{|aSub| aSub[F_NAME]==xFile} )
END
IF nPos>0
dRet:=SELF:_Dir[nPos, F_DATE]
ENDIF
RETURN dRet
METHOD GetDirectory(sWildcard) CLASS MC_FTP
RETURN SELF:_Dir:=SELF:Directory(sWildCard)
METHOD GetLowLevel(sFile,sNewFile,lFailIfExists,nFlags) CLASS MC_Ftp
LOCAL hfFTP AS PTR
LOCAL lRetVal AS LOGIC
LOCAL n AS DWORD
LOCAL hfLocal AS PTR
LOCAL pBuff AS PTR
LOCAL nSize AS LONGINT
LOCAL sSize AS STRING
LOCAL nDown AS LONGINT
LOCAL dwSecondsStart AS DWORD
LOCAL dwSecondsPassed AS DWORD
LOCAL dwSecondsTotal AS DWORD
LOCAL dwSecondsNeeded := 0 AS DWORD
LOCAL cCaption AS STRING
// Init vars
nSize:=SELF:GetSize(sFile)
nDown:=0
sSize:=AllTrim(AsString(Integer(nSize/1024)))
dwSecondsStart := DWORD(Seconds())
// Open een file
hfFTP :=SELF:OpenFile(sFile,GENERIC_READ)
SELF:Status := "Receiving file ..."
pBuff :=MemAlloc(MAX_BUFF_SIZE)
IF (hfFTP!=NULL_PTR .AND. pBuff!=NULL_PTR)
hfLocal:=FCreate(sNewFile)
IF hfLocal!=NULL_PTR
n :=MAX_BUFF_SIZE
lRetVal :=TRUE
// Set the range
IF IsAccess(SELF:_Owner,#FTPProgress)
SELF:_Owner:FTPProgress:Range:=Range{1,Integer(nSize/n)+2}
SELF:_Owner:FTPProgress:Show()
END
// Lees het bestand in
DO WHILE n=MAX_BUFF_SIZE
IF IsAccess(SELF:_Owner,#FTPStop)
IF SELF:_Owner:FTPStop
lRetVal:=FALSE
EXIT
END
END
IF ! InternetReadFile(hfFTP,pBuff,MAX_BUFF_SIZE,@n)
ToonMelding('W',SELF:InetErrorMsg(GetLastError()),SELF:_Owner)
EXIT
ENDIF
nDown+=n
IF n>0
FWrite3(hfLocal,pBuff,n)
ENDIF
IF IsAccess(SELF:_Owner,#FTPProgress)
SELF:_Owner:FTPProgress:Advance(1)
ENDIF
IF IsAccess(SELF:_Owner,#FTPProgressBytes)
IF ! SELF:_Owner:FTPProgressBytes:Caption ==
AllTrim(AsString(Integer(nDown/1024)))+"/"+sSize+" Kb"
SELF:_Owner:FTPProgressBytes:Caption :=
AllTrim(AsString(Integer(nDown/1024)))+"/"+sSize+" Kb"
ENDIF
ENDIF
IF IsAccess(SELF:_Owner,#FTPProgressTimeLeft) .AND. nDown >
2*MAX_BUFF_SIZE .AND. nSize > nDown .AND. (! DWORD(Seconds()) =
dwSecondsStart)
IF DWORD(Seconds()) < dwSecondsStart
*
* start was before midnight, now it's after
dwSecondsPassed := DWORD(Seconds())+DWORD(86399)-dwSecondsStart
ELSE
dwSecondsPassed := DWORD(Seconds())-dwSecondsStart
ENDIF
dwSecondsTotal := DWORD(DWORD(dwSecondsPassed*nSize)/nDown)
DO CASE
CASE IsNil(dwSecondsPassed)
*
* misbruik maken van VoError.log
SchrijfExeLog('VoError.log','GetLowLevel fout
gegaan:<.>IsNil(dwSecondsPassed)<.><9>Time(): '+Time()+'<.>')
CASE IsNil(dwSecondsTotal)
*
* misbruik maken van VoError.log
SchrijfExeLog('VoError.log','GetLowLevel fout
gegaan:<.>IsNil(dwSecondsTotal)<.><9>Time(): '+Time()+'<.>')
CASE dwSecondsTotal < dwSecondsPassed
*
* misbruik maken van VoError.log
SchrijfExeLog('VoError.log','GetLowLevel fout
gegaan:<.><9>dwSecondsPassed: '+NTrim(dwSecondsPassed)+'<.><9>nSize:
'+NTrim(nSize)+'<.><9>nDown:
'+NTrim(nDown)+'<.><9>DWORD(dwSecondsPassed*nSize):
'+NTrim(DWORD(dwSecondsPassed*nSize))+'<.><9>DWORD(DWORD(dwSecondsPassed*nSize)/nDown):
'+NTrim(DWORD(DWORD(dwSecondsPassed*nSize)/nDown))+'<.><9>Time():
'+Time()+'<.>')
OTHERWISE
dwSecondsNeeded := dwSecondsTotal-dwSecondsPassed
ENDCASE
*
* see if timer should be shown
//LogInfo('ADDINFO',Str(dwSecondsNeeded,5,0)+' >
'+Str(SELF:dwOldSecondsNeeded,5,0)+' .and.
'+Str(REAL4(dwSecondsNeeded),8,2)+' <
'+Str(REAL4(SELF:dwOldSecondsNeeded)*5/4,8,2))
IF dwSecondsNeeded > SELF:dwOldSecondsNeeded .AND.
REAL4(dwSecondsNeeded) < REAL4(SELF:dwOldSecondsNeeded)*5/4
// Avoid restless timer
ELSE
*
* store new seconds
SELF:dwOldSecondsNeeded := dwSecondsNeeded
*
* new caption
cCaption := ''
DO CASE
CASE dwSecondsNeeded > 60*60
cCaption += NTrim(Integer(dwSecondsNeeded/(60*60)))+':'
dwSecondsNeeded -= 60*60*Integer(dwSecondsNeeded/(60*60))
cCaption += StrZero(Integer(dwSecondsNeeded/60),2,0)+':'
dwSecondsNeeded -= 60*Integer(dwSecondsNeeded/60)
cCaption += StrZero(dwSecondsNeeded,2,0)
CASE dwSecondsNeeded > 60
cCaption += NTrim(Integer(dwSecondsNeeded/60))+':'
dwSecondsNeeded -= 60*Integer(dwSecondsNeeded/60)
cCaption += StrZero(dwSecondsNeeded,2,0)
CASE dwSecondsNeeded > 0
cCaption += NTrim(dwSecondsNeeded)
ENDCASE
*
* show timer
IF ! SELF:_Owner:FTPProgressTimeLeft:Caption == cCaption
SELF:_Owner:FTPProgressTimeLeft:Caption := cCaption
ENDIF
ENDIF
ENDIF
ApplicationExec(EXECWHILEEVENT)
ENDDO
// Sluit de boel!
FClose(hfLocal)
IF IsAccess(SELF:_Owner,#FTPProgress)
SELF:_Owner:FTPProgress:Position:=0
ENDIF
IF IsAccess(SELF:_Owner,#FTPProgressBytes)
SELF:_Owner:FTPProgressBytes:Caption:=NULL_STRING
ENDIF
SELF:CloseFile(hfFTP)
IF lRetVal
SELF:Status:="File downloaded"
ELSE
SELF:Status:="File not downloaded!"
ENDIF
ELSE
SELF:Error:=ERROR_INTERNET_INVALID_OPERATION
SELF:Status:="File "+sFile+"' niet received"
ENDIF
ENDIF
MemFree(pBuff)
RETURN lRetVal
METHOD GetName(xFile) CLASS MC_Ftp
LOCAL nPos AS INT
LOCAL sRet AS STRING
DO CASE
CASE IsNumeric(xFile)
nPos:=xFile
OTHERWISE
nPos:=AScan(SELF:_Dir,{|aSub| aSub[F_NAME]==xFile})
END
IF nPos>0
sRet:=SELF:_Dir[nPos,F_NAME]
ENDIF
RETURN sRet
METHOD GetSize(xFile) CLASS MC_Ftp
LOCAL nPos AS INT
LOCAL nRet AS INT
DO CASE
CASE IsNumeric(xFile)
nPos := xFile
OTHERWISE
nPos := AScan(SELF:_Dir,{|aSub| aSub[F_NAME]==xFile} )
ENDCASE
IF nPos > 0
nRet:=SELF:_Dir[nPos, F_SIZE]
ENDIF
RETURN nRet
METHOD GetTime(xFile) CLASS MC_Ftp
LOCAL nPos AS INT
LOCAL sRet AS STRING
DO CASE
CASE IsNumeric(xFile)
nPos:=xFile
OTHERWISE
nPos:=AScan(SELF:_Dir,{|aSub| aSub[F_NAME]==xFile} )
END
IF nPos>0
sRet:=SELF:_Dir[nPos, F_TIME]
ENDIF
RETURN sRet
METHOD InetErrorMsg(nError) CLASS MC_FTP
LOCAL cRet AS STRING
LOCAL pMsg AS PSZ
IF nError>0
FormatMessage(_OR(FORMAT_MESSAGE_ALLOCATE_BUFFER,
FORMAT_MESSAGE_FROM_SYSTEM), ;
NULL_PTR, nError, 0, @pMsg, 0, NULL_PTR)
cRet:=Psz2String(pMsg)
LocalFree(pMsg)
IF SLen(cRet) = 0
cRet:="Internet Error " + NTrim(nError)
ENDIF
ELSE
cRet:=""
ENDIF
RETURN cRet
METHOD Init(oOwner,cCaption, n, lStat, lNoPassive) CLASS MC_Ftp
// FTPStatus String Assign
// FTPProgress ProgressBar Access
// FTPAddInfo - Method
SUPER:Init(cCaption, n, lStat, lNoPassive)
SELF:_Owner := oOwner
SELF:_Status := NULL_STRING
SELF:_Dir := ArrayCreate(0)
RETURN SELF
METHOD InternetStatus(nContext, nStatus, pStatusInfo, nStatusLength)
CLASS MC_FTP
//
// This method receives all Low Level FTP notification. Please keep all
// parameters if you want to overwrite it for your own purpose
//
LOCAL cMsg AS STRING
SELF:_PreventShow:=FALSE
DO CASE
CASE nStatus == INTERNET_STATUS_RESOLVING_NAME
cMsg := "Seek FTP site... "
CASE nStatus == INTERNET_STATUS_NAME_RESOLVED
cMsg := "FTP Site found"
CASE nStatus == INTERNET_STATUS_CONNECTING_TO_SERVER
cMsg := "Make connection with server..."
CASE nStatus == INTERNET_STATUS_CONNECTED_TO_SERVER
cMsg := "Connection made with server"
CASE nStatus == INTERNET_STATUS_SENDING_REQUEST
cMsg := "Send request..."
SELF:_PreventShow:=TRUE
CASE nStatus == INTERNET_STATUS_REQUEST_SENT
cMsg := "Request send"
SELF:_PreventShow:=TRUE
CASE nStatus == INTERNET_STATUS_RECEIVING_RESPONSE
cMsg := "Busy receiving..."
SELF:_PreventShow:=TRUE
CASE nStatus == INTERNET_STATUS_RESPONSE_RECEIVED
cMsg := "Answer received"
SELF:_PreventShow:=TRUE
CASE nStatus == INTERNET_STATUS_CTL_RESPONSE_RECEIVED
cMsg := "CTL answer received"
CASE nStatus == INTERNET_STATUS_PREFETCH
cMsg := "Prefetch"
CASE nStatus == INTERNET_STATUS_CLOSING_CONNECTION
cMsg := "Close connection..."
CASE nStatus == INTERNET_STATUS_CONNECTION_CLOSED
cMsg := "Connection closed"
CASE nStatus == INTERNET_STATUS_HANDLE_CREATED
cMsg := "Handle created"
RETURN SELF
CASE nStatus == INTERNET_STATUS_HANDLE_CLOSING
cMsg := "Close handle ..."
CASE nStatus == INTERNET_STATUS_REQUEST_COMPLETE
cMsg := "Request done"
CASE nStatus == INTERNET_STATUS_REDIRECT
cMsg := "Redirect"
OTHERWISE
cMsg := "Unknown FTP Status"
ENDCASE
// Zet de status
SELF:Status:=cMsg
ApplicationExec(EXECWHILEEVENT)
RETURN SELF
ACCESS Owner CLASS MC_FTP
RETURN SELF:_Owner
METHOD PutFile(cLocalFile, cRemoteFile, lFailIfExists, nFlags) CLASS MC_Ftp
LOCAL lRetVal AS LOGIC
lRetVal:=SUPER:PutFile(cLocalFile, cRemoteFile, lFailIfExists, nFlags)
RETURN lRetVal
METHOD PutLowLevel(sFile, cRemoteFile, lFailIfExists, nFlags) CLASS MC_FTP
LOCAL hfFTP AS PTR
LOCAL lRet AS LOGIC
LOCAL n AS DWORD
LOCAL hfLocal AS PTR
LOCAL DIM abBuff[MAX_BUFF_SIZE] AS BYTE
LOCAL nSize AS LONGINT
LOCAL sSize AS STRING
LOCAL nUp AS DWORD
LOCAL dwSecondsStart AS DWORD
LOCAL dwSecondsPassed AS DWORD
LOCAL dwSecondsNeeded AS DWORD
LOCAL cCaption AS STRING
// Init vars
hfLocal:=FOpen(sFile)
nUp:=0
FFirst(sFile,FC_NORMAL)
nSize:=FSize()
sSize:=AllTrim(AsString(Integer(nSize/1024)))
dwSecondsStart := DWORD(Seconds())
IF hfLocal!=NULL_PTR
SELF:Status:="Busy sending.."
hfFTP:=SELF:OpenFile(cRemoteFile,GENERIC_WRITE)
IF hfFTP != NULL_PTR
n :=MAX_BUFF_SIZE
lRet:=.T.
// Set de progressbar
IF IsAccess(SELF:_Owner,#FTPProgress)
SELF:_Owner:FTPProgress:Range:=Range{1,Integer(nSize/n)+2}
SELF:_Owner:FTPProgress:Show()
END
// Schrijf het bestand
DO WHILE n=MAX_BUFF_SIZE
IF IsAccess(SELF:_Owner,#FTPStop)
IF SELF:_Owner:FTPStop
lRet:=FALSE
EXIT
END
END
n:=FRead3(hfLocal,@abBuff[1],n)
IF n>0
IF !InternetWriteFile(hfFTP, @abBuff[1], n, @n)
SELF:Error:=GetLastError()
EXIT
ENDIF
ENDIF
nUp+=n
IF IsAccess(SELF:_Owner,#FTPProgress)
SELF:_Owner:FTPProgress:Advance(1)
END
IF IsAccess(SELF:_Owner,#FTPProgressBytes)
IF ! SELF:_Owner:FTPProgressBytes:Caption ==
AllTrim(AsString(Integer(nUp/1024)))+"/"+sSize+" Kb"
SELF:_Owner:FTPProgressBytes:Caption :=
AllTrim(AsString(Integer(nUp/1024)))+"/"+sSize+" Kb"
ENDIF
ENDIF
IF IsAccess(SELF:_Owner,#FTPProgressTimeLeft) .AND. nUp > 0 .AND.
nSize > nUp .AND. (! DWORD(Seconds()) = dwSecondsStart)
IF DWORD(Seconds()) < dwSecondsStart
*
* start was before midnight, now it's after
dwSecondsPassed := DWORD(Seconds())+DWORD(86399)-dwSecondsStart
ELSE
dwSecondsPassed := DWORD(Seconds())-dwSecondsStart
ENDIF
dwSecondsNeeded := DWORD(nSize*dwSecondsPassed/nUp)-dwSecondsPassed
*
* see if timer should be shown
//LogInfo('ADDINFO',Str(dwSecondsNeeded,5,0)+' >
'+Str(SELF:dwOldSecondsNeeded,5,0)+' .and.
'+Str(REAL4(dwSecondsNeeded),8,2)+' <
'+Str(REAL4(SELF:dwOldSecondsNeeded)*5/4,8,2))
IF dwSecondsNeeded > SELF:dwOldSecondsNeeded .AND.
REAL4(dwSecondsNeeded) < REAL4(SELF:dwOldSecondsNeeded)*5/4
// Avoid restless timer
ELSE
*
* store new seconds
SELF:dwOldSecondsNeeded := dwSecondsNeeded
*
* new caption
cCaption := ''
DO CASE
CASE dwSecondsNeeded > 60*60
cCaption += NTrim(Integer(dwSecondsNeeded/(60*60)))+':'
dwSecondsNeeded -= 60*60*Integer(dwSecondsNeeded/(60*60))
cCaption += StrZero(Integer(dwSecondsNeeded/60),2,0)+':'
dwSecondsNeeded -= 60*Integer(dwSecondsNeeded/60)
cCaption += StrZero(dwSecondsNeeded,2,0)
CASE dwSecondsNeeded > 60
cCaption += NTrim(Integer(dwSecondsNeeded/60))+':'
dwSecondsNeeded -= 60*Integer(dwSecondsNeeded/60)
cCaption += StrZero(dwSecondsNeeded,2,0)
CASE dwSecondsNeeded > 0
cCaption += NTrim(dwSecondsNeeded)
ENDCASE
*
* show timer
IF ! SELF:_Owner:FTPProgressTimeLeft:Caption == cCaption
SELF:_Owner:FTPProgressTimeLeft:Caption := cCaption
ENDIF
ENDIF
ENDIF
ApplicationExec(EXECWHILEEVENT)
ENDDO
IF IsAccess(SELF:_Owner,#FTPProgress)
SELF:_Owner:FTPProgress:Position := 0
END
SELF:CloseFile(hfFTP)
IF !lRet
// Transport mislukt.. Delete the file!
SELF:Status:="Verwijder partiele upload!"
SELF:DeleteFile(cRemoteFile)
END
SELF:Status:="File send!"
ELSE
SELF:Error :=ERROR_INTERNET_INVALID_OPERATION
SELF:Status :="File not send!"
ENDIF
FClose(hfLocal)
ENDIF
RETURN lRet
ACCESS Status CLASS MC_FTP
RETURN SELF:_Status
ASSIGN Status(sStatus) CLASS MC_FTP
// Set the range
IF IsMethod(SELF:_Owner,#FTPAddInfo) .AND. !SELF:_PreventShow
SELF:_Owner:FtpAddInfo(sStatus)
END
RETURN SELF:_Status:=sStatus
METHOD ConnectProxy(cAgent,cHost,cUserID,cPassword,cProxyAddress,nPort)
CLASS MC_Ftp
LOCAL lReturn AS LOGIC
SELF:hSession:=SELF:OpenProxySession(cAgent,cProxyAddress,nPort)
IF SELF:hSession!=NULL_PTR
SELF:hConnect:=SELF:ConnectFTPSite(SELF:hSession,cHost,cUserID,cPassword)
IF SELF:hConnect!=NULL_PTR
lReturn:=TRUE
ENDIF
ENDIF
RETURN lReturn
METHOD OpenProxySession(sAgent,sProxyAddress,nProxyPort) CLASS MC_Ftp
LOCAL hSession AS PTR
hSession:=InternetOpen( String2Psz(sAgent),;
DWORD(INTERNET_OPEN_TYPE_PROXY),;
String2Psz("ftp=ftp://"+sProxyAddress+":"+LTrim(Str(nProxyPort))),;
NULL_PSZ,;
DWORD(0) )
RETURN hSession
METHOD ConnectFTPSite(hSession,sHost,sUser,sPassword) CLASS MC_Ftp
LOCAL hConnect AS PTR
hConnect:=InternetConnect( hSession,;
String2Psz(sHost),;
0,;
String2Psz(sUser),;
String2Psz(sPassword),;
INTERNET_SERVICE_FTP,;
0,;
0 )
RETURN hConnect
DEFINE MAX_BUFF_SIZE :=2500
Martin schreef op 8-12-2014 om 12:39:
> We need to look at FTP and first stage is to see if anyone has done it
> in VO and if this is practical.
>
> Thanks
>
> Martin
Back to comp.lang.clipper.visual-objects | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
FTP in VO, any samples please? Martin <spam@spam.spam> - 2014-12-08 11:39 +0000
Re: FTP in VO, any samples please? JohnMartens <martens@tennishulp.info> - 2014-12-08 12:54 +0100
Re: FTP in VO, any samples please? Karl Faller <mail_withoutthat_@_drei-f_andthat_.de> - 2014-12-08 14:15 +0100
Re: FTP in VO, any samples please? "Marc Verkade [MITCon]" <marc@mitNOSPAMconDELETE.nl> - 2014-12-15 16:57 +0100
Re: FTP in VO, any samples please? Martin <spam@spam.spam> - 2014-12-18 16:23 +0000
csiph-web