FTP read/copy file
Moderator: Enrico Maria Giordano
-
- Posts: 76
- Joined: Wed Oct 12, 2005 5:52 pm
- Location: Milan,Italy
FTP read/copy file
Ho cambiato PC e non mi ritrovo un piccolo ma utile esempio di download/upoload di un file con FTP che avevo scaricato dal forum di fwin.
Ricordo che era stato fatto utilizzando la classe TFTPFILE.PRG
Qualcuno se lo ritrova in giro ?
Grazie
Piscicelli/Zingoni
Ricordo che era stato fatto utilizzando la classe TFTPFILE.PRG
Qualcuno se lo ritrova in giro ?
Grazie
Piscicelli/Zingoni
- Enrico Maria Giordano
- Posts: 8777
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Has thanked: 1 time
- Been thanked: 8 times
- Contact:
Re: FTP read/copy file
Questo usa direttamente le API:
EMG
Code: Select all | Expand
#include "Fivewin.ch"//// File attributes//#define FILE_ATTRIBUTE_READONLY 1#define FILE_ATTRIBUTE_HIDDEN 2#define FILE_ATTRIBUTE_SYSTEM 4#define FILE_ATTRIBUTE_DIRECTORY 16#define FILE_ATTRIBUTE_ARCHIVE 32#define FILE_ATTRIBUTE_NORMAL 128#define FILE_ATTRIBUTE_TEMPORARY 256//// access types for InternetOpen()//#define INTERNET_OPEN_TYPE_PRECONFIG 0 // use registry configuration#define INTERNET_OPEN_TYPE_DIRECT 1 // direct to net#define INTERNET_OPEN_TYPE_PROXY 3 // via named proxy#define INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY 4 // prevent using java/script/INS//// manifests//#define INTERNET_INVALID_PORT_NUMBER 0 // use the protocol-specific default#define INTERNET_DEFAULT_FTP_PORT 21 // default for FTP servers#define INTERNET_DEFAULT_GOPHER_PORT 70 // " " gopher "#define INTERNET_DEFAULT_HTTP_PORT 80 // " " HTTP "#define INTERNET_DEFAULT_HTTPS_PORT 443 // " " HTTPS "#define INTERNET_DEFAULT_SOCKS_PORT 1080 // default for SOCKS firewall servers.//// service types for InternetConnect()//#define INTERNET_SERVICE_FTP 1#define INTERNET_SERVICE_GOPHER 2#define INTERNET_SERVICE_HTTP 3//// flags for FTP//#define INTERNET_FLAG_TRANSFER_ASCII 1#define INTERNET_FLAG_TRANSFER_BINARY 2STATIC hLibFUNCTION MAIN() LOCAL hInternet, hConnect hLib = LOADLIB32( "wininet.dll" ) hInternet = INETOPEN( "Anystring", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0 ) hConnect = INETCONNECT( hInternet, "ftp address", INTERNET_INVALID_PORT_NUMBER, "userid", "password", INTERNET_SERVICE_FTP, 0, 0 ) ? FTPGETFILE( hConnect, "remote file", "local file", 0, FILE_ATTRIBUTE_ARCHIVE, 0, 0 )// ? FTPPUTFILE( hConnect, "local file", "remote file", 0, 0 ) INETCLOSEHANDLE( hConnect ) INETCLOSEHANDLE( hInternet ) FREELIB32( hLib ) RETURN NILDLL32 FUNCTION INETOPEN( cAgent AS LPSTR, nAccessType AS DWORD, cProxyName AS LPSTR, cProxyBypass AS LPSTR, nFlags AS DWORD ) AS LONG; PASCAL FROM "InternetOpenA" LIB hLibDLL32 FUNCTION INETCLOSEHANDLE( hInternet AS LONG ) AS BOOL; PASCAL FROM "InternetCloseHandle" LIB hLibDLL32 FUNCTION INETCONNECT( hInternet AS LONG, cServerName AS LPSTR, nServerPort AS LONG, cUserName AS LPSTR, cPassword AS LPSTR, nService AS DWORD, nFlags AS DWORD, @nContext AS PTR ) AS LONG; PASCAL FROM "InternetConnectA" LIB hLibDLL32 FUNCTION FTPGETFILE( hConnect AS LONG, cRemoteFile AS LPSTR, cNewFile AS LPSTR, nFailIfExists AS LONG, nFlagsAndAttribs AS DWORD, nFlags AS DWORD, @nContext AS PTR ) AS BOOL; PASCAL FROM "FtpGetFileA" LIB hLibDLL32 FUNCTION FTPPUTFILE( hConnect AS LONG, cLocalFile AS LPSTR, cNewRemoteFile AS LPSTR, nFlags AS DWORD, @nContext AS PTR ) AS BOOL; PASCAL FROM "FtpPutFileA" LIB hLib
EMG
-
- Posts: 76
- Joined: Wed Oct 12, 2005 5:52 pm
- Location: Milan,Italy
- Enrico Maria Giordano
- Posts: 8777
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Has thanked: 1 time
- Been thanked: 8 times
- Contact:
Potresti utilizzare un trucco: una progressbar che avanza con il tempo e ricomincia una volta che è arrivata in fondo. 
Altrimenti sei costretto ad utilizzare le funzioni analoghe alla FOpen() e compagnia. Ho questo esempio ma non l'ho scritto io e non so neanche se funziona:
EMG

Altrimenti sei costretto ad utilizzare le funzioni analoghe alla FOpen() e compagnia. Ho questo esempio ma non l'ho scritto io e non so neanche se funziona:
Code: Select all | Expand
#include "Fivewin.ch"#include "Struct.ch"//// access types for InternetOpen()//#define INTERNET_OPEN_TYPE_PRECONFIG 0 // use registry configuration#define INTERNET_OPEN_TYPE_DIRECT 1 // direct tonet#define INTERNET_OPEN_TYPE_PROXY 3 // via named proxy#define INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY 4 // prevent using java/script/INS//// manifests//#define INTERNET_INVALID_PORT_NUMBER 0 // use the protocol-specific default#define INTERNET_DEFAULT_FTP_PORT 21 // default for FTP servers#define INTERNET_DEFAULT_GOPHER_PORT 70 // " " gopher "#define INTERNET_DEFAULT_HTTP_PORT 80 // " " HTTP "#define INTERNET_DEFAULT_HTTPS_PORT 443 // " " HTTPS "#define INTERNET_DEFAULT_SOCKS_PORT 1080 // default for SOCKS firewall servers.#define GENERIC_READ 2147483648 // 0x80000000#define GENERIC_WRITE 1073741824 // 0x40000000#define FTP_TRANSFER_TYPE_ASCII 1#define FTP_TRANSFER_TYPE_BINARY 2#define FILE_BEGIN 0#define FILE_CURRENT 1#define FILE_END 2//// service types for InternetConnect()//#define INTERNET_SERVICE_FTP 1#define INTERNET_SERVICE_GOPHER 2#define INTERNET_SERVICE_HTTP 3//// flags for FTP//#define INTERNET_FLAG_TRANSFER_ASCII 1#define INTERNET_FLAG_TRANSFER_BINARY 2#define FILE_ATTRIBUTE_ARCHIVE 0STATIC hLibFunction Main local nActual := 0, oMeter,oDlg local cText:="n.1" local osay1 FERASE("FTP.LOG") DEFINE DIALOG oDlg FROM 1, 1 TO 20, 44 ; TITLE "Testing Ftp with meter" @ 1, 1 SAY oSay1 prompt cText OF oDlg @ 2, 2 METER oMeter VAR nActual TOTAL 100 OF oDlg SIZE 135, 40 @ 5, 12 BUTTON "&Start transfer" OF oDlg size 80,40; action(StartTransfer(oMeter,oSay1)) ACTIVATE WINDOW oDlgreturn nilFUNCTION STARTTRANSFER(oMeter,osay1) local cFsrv:="www.softwarexp.co.uk" local cFusr:="ftpbackup" local cFpsw:="backservice" local cFpth:="/pigc/" local cFlocal:="ftptest.prg" local cFremote:="ftptest.prg" LOCAL I FOR I:=1 TO 50 oSay1:SetText(str(i,4)) FtpWriteFile(cFsrv,cFusr,cFpsw,cFlocal,cFpth+cFremote,oMeter,1,100) NEXT return nilFUNCTION FtpWriteFile(cFtpSite,cFtpUser,cFtpPassword,cFtpLocal,cFtpRemote,oFtpMeter,nMeterStart,nMeterEnd) LOCAL hInternet, hConnect,lReturn, i local cBuffer:=space(128) local nBufSize:=128 local handle local nSize local nProgress local nWritten:=0 local lValue local nRange local hFile local nBytes nSize:=fsize(cFTPlocal) if oFTPMeter<>NIL nRange:=nMeterEnd-nMeterStart endif ftplog("") ftplog("Session FTPWRITEFILE - Site: "+cFtpRemote+" Local file:"+cFtpLocal+" Remote file: "+cFtpRemote) ftplog("Loading Wininet.dll") hLib = LOADLIB32( "wininet.dll" ) ftplog("Done. Handle:"+str(hLib,10)) ftplog("Calling InetOpen") hInternet = INETOPEN( "Anystring", 0, 0, 0, 0 ) ftplog("Done. Handle:"+str(hInternet,10)) ftplog("Calling hConnect") hConnect = INETCONNECT( hInternet, cFtpSite, 21, cFtpUser, cFtpPassword,INTERNET_SERVICE_FTP, 0, 0 ) ftplog("Done. Handle:"+str(hConnect,10)) ftplog("Calling FtpOpenFile") hFile := FtpOpenFile( hConnect, cFtpRemote, GENERIC_WRITE, FTP_TRANSFER_TYPE_BINARY, 0 ) ftplog("Done. Handle:"+str(hFile,10)) nProgress:=0 handle:=fopen(cFtpLocal) while ( nBytes := FRead( handle, @cBuffer, nBufSize ) ) > 0 lReturn:=InternetWriteFile(hFile, cBuffer, nBytes, @nWritten ) if lReturn=.f. msginfo("Write error.") endif nProgress:=nProgress+nWritten ftplog("Wrote "+alltrim(str(nProgress,10))+" of "+alltrim(str(nSize,10))) if oFTPMeter<>NIL oFTPMeter:Set(nMeterStart+(nProgress*nRange)/nSize) endif end FClose( handle ) lValue:=INETCLOSE( hFile ) if lValue ftplog("Closing FtpOpenFile. Ok.") else ftplog("Closing FtpOpenFile. Error.") endif lValue:=INETCLOSE( hConnect ) if lValue ftplog("Closing HCONNECT. Ok.") else ftplog("Closing HCONNECT. Error.") endif lValue:=INETCLOSE( hInternet ) if lValue ftplog("Closing HINTERNET. Ok.") else ftplog("Closing HINTERNET. Error.") endif lValue:=FREELIB32( hLib ) if lValue<>0 ftplog("Closing WININET. Ok.") else ftplog("Closing WININET. Error.") endif RETURN lReturnFUNCTION FtpReadFile(cFtpSite,cFtpUser,cFtpPassword,cFtpLocal,cFtpRemote,oFTPmeter,nMeterStart,nMeterEnd) LOCAL hInternet, hConnect,lReturn, i LOCAL nBytes:=128 LOCAL cBuffer LOCAL nRead LOCAL nRange LOCAL nSize LOCAL nProgress:=0 local handle local lValue local hFile if oFTPMeter<>NIL nRange:=nMeterEnd-nMeterStart endif ftplog("") ftplog("Session FTPREADFILE - Site: "+cFtpRemote+" Local file:"+cFtpLocal+" Remote file: "+cFtpRemote) ftplog("Loading Wininet.dll") hLib = LOADLIB32( "wininet.dll" ) ftplog("Done. Handle:"+str(hLib,10)) ftplog("Calling InetOpen") hInternet = INETOPEN( "Anystring", 0, 0, 0, 0 ) ftplog("Done. Handle:"+str(hInternet,10)) ftplog("Calling hConnect") hConnect = INETCONNECT( hInternet, cFtpSite, 21, cFtpUser, cFtpPassword,INTERNET_SERVICE_FTP, 0, 0 ) ftplog("Done. Handle:"+str(hConnect,10)) ftplog("Calling FTPFILESIZE") nSize:=FtpFileSize(cFtpRemote,hConnect) ftplog("Done. File size is "+str(nSize,10)) ferase(cFtpLocal) if nSize=0 ftplog("Remote file did not find. Bypass reading and return .f.") lReturn:=.f. else ftplog("Calling FTPOPENFILE") hFile := FtpOpenFile( hConnect, cFtpRemote, GENERIC_READ, FTP_TRANSFER_TYPE_BINARY, 0 ) if hFile=0 ftplog("Error. Handle:"+str(hFile,10)) lReturn:=.f. else ftplog("Done. Handle:"+str(hFile,10)) cBuffer = Space( nBytes ) handle:=fcreate(cFtpLocal) if handle=-1 msginfo("Unable to create dest file "+cFtpLocal) return(.f.) endif do while .t. lReturn:=InternetReadFile(hFile,cBuffer,nBytes,@nRead) if lReturn=.f. msginfo("errore") exit endif fwrite(handle,cBuffer,nRead) nProgress:=nProgress+nRead ftplog("Read "+alltrim(str(nProgress,10))+" of "+alltrim(str(nSize,10))) if nProgress=nSize exit endif if oFTPMeter<>NIL oFTPMeter:SetPos(nMeterStart+(nProgress*nRange)/nSize) endif enddo fclose(handle) lValue:=INETCLOSE( hFile ) if lValue ftplog("Closing FTPOPENFILE. Ok.") else ftplog("Closing FTPOPENFILE. Error.") endif endif endif lValue:=INETCLOSE( hConnect ) if lValue ftplog("Closing HCONNECT. Ok.") else ftplog("Closing HCONNECT. Error.") endif lValue:=INETCLOSE( hInternet ) if lValue ftplog("Closing HINTERNET. Ok.") else ftplog("Closing HINTERNET. Error.") endif lValue:=FREELIB32( hLib ) if lValue<>0 ftplog("Closing WININET. Ok.") else ftplog("Closing WININET. Error.") endif RETURN lReturnFUNCTION FTPDIR( cFtpSite,cFtpUser,cFtpPassword,cMask ) local hFTPDir, aFiles := {} local oWin32FindData, cBuffer LOCAL hInternet, hConnect,lReturn, i LOCAL nBytes:=2000 LOCAL nRead LOCAL nRange LOCAL nSize LOCAL nProgress:=0 local handle local npos local lValue DEFAULT cMask := "*.*" ftplog("") ftplog("Function FTPDIR "+cMask) ftplog("Loading Wininet.dll") hLib = LOADLIB32( "wininet.dll" ) ftplog("Done. Handle:"+str(hLib,10)) ftplog("Calling InetOpen") hInternet = INETOPEN( "Anystring", 0, 0, 0, 0 ) ftplog("Done. Handle:"+str(hInternet,10)) ftplog("Calling hConnect") hConnect = INETCONNECT( hInternet, cFtpSite, 21, cFtpUser, cFtpPassword,INTERNET_SERVICE_FTP, 0, 0 ) ftplog("Done. Handle:"+str(hConnect,10)) STRUCT oWin32FindData MEMBER nFileAttributes AS DWORD MEMBER nCreationTime AS STRING LEN 8 MEMBER nLastReadAccess AS STRING LEN 8 MEMBER nLastWriteAccess AS STRING LEN 8 MEMBER nSizeHight AS DWORD MEMBER nSizeLow AS DWORD MEMBER nReserved0 AS DWORD MEMBER nReserved1 AS DWORD MEMBER cFileName AS STRING LEN 260 MEMBER cAltName AS STRING LEN 18 ENDSTRUCT cBuffer = oWin32FindData:cBuffer ftplog("Calling FtpFindFirstFile") hFTPDir = FtpFindFirstFile( hconnect, cMask, @cBuffer, 0, 0 ) ftplog("Done. Handle:"+str(hFtpDir,10)) oWin32FindData:cBuffer = cBuffer if ! Empty( oWin32FindData:cFileName ) AAdd( aFiles, { oWin32FindData:cFileName,; oWin32FindData:nSizeLow } ) while InternetFindNextFile( hFTPDir, @cBuffer ) oWin32FindData:cBuffer = cBuffer AAdd( aFiles, { oWin32FindData:cFileName,; oWin32FindData:nSizeLow } ) end endif lValue:=INETCLOSE( hFTPDir ) if lValue ftplog("Closing FtpFindFirstFile. Ok.") else ftplog("Closing FtpFindFirstFile. Error.") endif lValue:=INETCLOSE( hConnect ) if lValue ftplog("Closing HCONNECT. Ok.") else ftplog("Closing HCONNECT. Error.") endif lValue:=INETCLOSE( hInternet ) if lValue ftplog("Closing HINTERNET. Ok.") else ftplog("Closing HINTERNET. Error.") endif lValue:=FREELIB32( hLib ) if lValue<>0 ftplog("Closing WININET. Ok.") else ftplog("Closing WININET. Error.") endif for i:=1 to len(aFiles) nPos:=at(chr(0),afiles[i,1]) if nPos>0 aFiles[i,1]:=substr(aFiles[i,1],1,nPos-1) endif next RETURN aFilesreturn aFilesFUNCTION FTPFILESIZE( cFTPRemote, hConnect ) local hFTPDir, aFiles := {} local oWin32FindData, cBuffer local lValue STRUCT oWin32FindData MEMBER nFileAttributes AS DWORD MEMBER nCreationTime AS STRING LEN 8 MEMBER nLastReadAccess AS STRING LEN 8 MEMBER nLastWriteAccess AS STRING LEN 8 MEMBER nSizeHight AS DWORD MEMBER nSizeLow AS DWORD MEMBER nReserved0 AS DWORD MEMBER nReserved1 AS DWORD MEMBER cFileName AS STRING LEN 260 MEMBER cAltName AS STRING LEN 18 ENDSTRUCT cBuffer = oWin32FindData:cBuffer ftplog("Calling FtpFindFirstFile") hFTPDir = FtpFindFirstFile( hConnect, cFTPRemote, @cBuffer, 0, 0 ) if hFTPDir=0 ftplog("Error. Handle not valid:"+str(hftpDIR,10)) else ftplog("Done. Handle:"+str(hftpDIR,10)) oWin32FindData:cBuffer = cBuffer if ! Empty( oWin32FindData:cFileName ) AAdd( aFiles, { oWin32FindData:cFileName,; oWin32FindData:nSizeLow } ) endif lValue:=INETCLOSE( hFTPDir ) if lValue ftplog("Closing FtpFindFirstFile. Ok.") else ftplog("Closing FtpFindFirstFile. Error.") endif endif if len(aFiles)>0 return(aFiles[1,2]) endif return(0)function FTPlog(cFtpVar)local ftphandle if .not. file("ftp.log") ftpHandle:=fcreate("ftp.log") else ftpHandle:=fopen("ftp.log",2) fseek(FTPhandle,0,2) endiffwrite(ftphandle,dtoc(date())+" "+time()+" "+cFtpVar+chr(13)+chr(10))fclose(ftphandle)return nilfunction inetclose(hInternet)if hInternet<>NIL return(inetclosehandle(hInternet))endifreturn(0)DLL32 FUNCTION FtpFindFirstFile( hFTP AS LONG, cMask AS LPSTR,; @cWin32DataInfo AS LPSTR, n1 AS LONG, n2 AS LONG ) ; AS LONG PASCAL FROM "FtpFindFirstFileA" LIB hLibDLL32 FUNCTION InternetFindNextFile( hFTPDir AS LONG, @cWin32DataInfo AS LPSTR ) ; AS BOOL PASCAL FROM "InternetFindNextFileA" LIB hLibDLL32 FUNCTION INETOPEN( cAgent AS LPSTR, nAccessType AS DWORD, cProxyName AS LPSTR, cProxyBypass AS LPSTR, nFlags AS DWORD ) AS LONG; PASCAL FROM "InternetOpenA" LIB hLibDLL32 FUNCTION INETCLOSEHANDLE( hInternet AS LONG ) AS BOOL; PASCAL FROM "InternetCloseHandle" LIB hLibDLL32 FUNCTION INETCONNECT( hInternet AS LONG, cServerName AS LPSTR, nServerPort AS LONG, cUserName AS LPSTR, cPassword AS LPSTR, nService AS DWORD, nFlags AS DWORD, @nContext AS PTR ) AS LONG; PASCAL FROM "InternetConnectA" LIB hLibDLL32 FUNCTION FTPGETFILE( hConnect AS LONG, cRemoteFile AS LPSTR,cNewFile AS LPSTR, nFailIfExists AS LONG, nFlagsAndAttribs AS DWORD,nFlags AS DWORD, @nContext AS PTR ) AS BOOL; PASCAL FROM "FtpGetFileA" LIB hLibDLL32 FUNCTION FTPPUTFILE( hConnect AS LONG, cLocalFile AS LPSTR,cNewRemoteFile AS LPSTR, nFlags AS DWORD, @nContext AS PTR ) AS BOOL; PASCAL FROM "FtpPutFileA" LIB hLibDLL32 FUNCTION InternetWriteFile( hFile AS LONG, cBuffer AS LPSTR, lSize AS LONG,; @nSize AS PTR ) AS BOOL PASCAL LIB hLibDLL32 FUNCTION FtpOpenFile( hFTP AS LONG, cRemoteFile AS LPSTR, n1 AS LONG,; n2 AS LONG, n3 AS LONG ) AS LONG PASCAL ; FROM "FtpOpenFileA" LIB hLibDLL32 FUNCTION InternetReadFile( hFile AS LONG, cBuffer AS LPSTR, lSize AS LONG,; @nSize AS PTR ) AS BOOL PASCAL LIB hLibDLL32 FUNCTION InternetSetFilePointer( hFile AS LONG, nDistanceToMove AS LONG, nReserved AS LPSTR, nSeekMethod AS LONG,; @nContext AS PTR ) AS BOOL PASCAL LIB hLib
EMG
-
- Posts: 76
- Joined: Wed Oct 12, 2005 5:52 pm
- Location: Milan,Italy
Come faccio con il trucco se, dai miei test, nel momento che parto con FTPGEFILE(...) con un file di 2 mega e vecchio modem,il programma non mi da piu' retta se non alla fine del download (anche utilizzando un timer) e quindi al cliente sembra che si sia inchiodato.
Hai forse qualche trucco super!
Tengo comunque da parte l'altro prg che mi ha dato.
Grazie
Piscicelli/Zingoni
Hai forse qualche trucco super!
Tengo comunque da parte l'altro prg che mi ha dato.
Grazie
Piscicelli/Zingoni
- Enrico Maria Giordano
- Posts: 8777
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Has thanked: 1 time
- Been thanked: 8 times
- Contact:
-
- Posts: 76
- Joined: Wed Oct 12, 2005 5:52 pm
- Location: Milan,Italy
- Marco Turco
- Posts: 858
- Joined: Fri Oct 07, 2005 12:00 pm
- Location: London
- Contact:
-
- Posts: 76
- Joined: Wed Oct 12, 2005 5:52 pm
- Location: Milan,Italy
Ho provato a compilare l'esempio di EMG con fwh e mi da i seg. errori:
Error E2449 ftp-news.c 150: Size of 'hb_vm_SymbolInit_FTP' is unknown or zero
Error E2141 ftp-news.c 150: Declaration syntax error
Error E2109 ftp-news.c 162: Not an allowed type
Per me e' Arabo puro.
Che roba e'
Forse quell'esempio postato e' da rivedere.
Ciao
Error E2449 ftp-news.c 150: Size of 'hb_vm_SymbolInit_FTP' is unknown or zero
Error E2141 ftp-news.c 150: Declaration syntax error
Error E2109 ftp-news.c 162: Not an allowed type
Per me e' Arabo puro.
Che roba e'
Forse quell'esempio postato e' da rivedere.
Ciao
- Enrico Maria Giordano
- Posts: 8777
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Has thanked: 1 time
- Been thanked: 8 times
- Contact: