ven. 25 mai 2012, 13h01
Gregory
atoutfox.public.association
Re: PB ouverture fichier sur serveur FTP classe ANATOLE
Gilles,
Essaye de modifier
p_ftp.mConnect = InternetConnect (p_ftp.mOpen, p_connexion,;
INTERNET_INVALID_PORT_NUMBER,;
p_login, p_motpasse, INTERNET_SERVICE_FTP, 0, 0)
par
#define INTERNET_FLAG_PASSIVE 0x08000000
p_ftp.mConnect = InternetConnect (p_ftp.mOpen, p_connexion,;
INTERNET_INVALID_PORT_NUMBER,;
p_login, p_motpasse, INTERNET_SERVICE_FTP, ;
INTERNET_FLAG_PASSIVE, ;
0)
______
Gregory
______
"GLS" wrote in message news:jpnis0$1aal$1@news.niouzes.net...
Bonjour,
J'ai un problème d'aptitude de la fonction ftp.ouvrirfichier(p1,p2) de la
classe d'ANATOLE qui reprend l'API WININET.dll pour gérer les accès FTP.
Avec le même serveur FTP hébergé chez WanAgain et la même application VFP
MYFTPBIO installée sur des ordinateurs différents (tous sous W7 familiale
SP1 avec AVAST et pare-feu autorisant l'application MYFTPBIO) situés à des
zones géographiques différentes donc passant par des routeurs ADSL
différents, j'ai des non exécution de la fonction ouvrifichier() pour
certaines machines et pas d'autres.
Je précise que toutes les opérations nécessaires à l'accès du serveur FTP
avant l'appel de la fonction ouvrirfichier() sont correctement effectuées
sur toutes les machines, seule la fonction ouvrirfichier() primordiale pour
la lecture ou l'écriture sur le serveur FTP ne fonctionnent que sur
certaines machines.
Je précise aussi que sur les machines défaillantes, l'accès FTP par
l'Internet Exploreur sur les serveur FTP WEN WanAgain fonctionne très bien.
Je précise encore qu'avec les outils de gestion FTP 'DART', l'accès FTP
fonctionne bien. cette bibilothèque externe n'utilise pas l'api WININET mais
ses propre s api(s) DARTSOCK et DARTFTP.
Enfin sur les PC "défaillants" la même application fonctionne très bien en
lecture/ecriture sur un serveur FTP connecté sur le réseau local..
Y aurait-il dans la fonction ouvrirfichier des flags supplémentaires à
ajouter pour le traitement de la fonction ftpopenfile() de la DLL WININET ?
Flags qui seraient à ajouter dans un certain contexte ?
Y-aurait-il un problème sur le pare-feu internet pour cette DLL WININET ?
D'avance merci pour votre aide
Cordialement
GLS
[VFP]
FUNCTION LOCAL_VERS_SERVEUR(p_ftp,p_fichier_local, p_fichier_serveur,
p_chemin_serveur, p_connexion, p_login, p_motpasse)
LOCAL lRetour, hFTP, cRepCour
*--------------------------------------------------------------------------------------------------------------------*
* FTP
* Fonction de transfert d'un fichier local vers un serveur FTP utilisant la
classe dérivée FTP d'ANATOLE sur ATOUTFOX
*--------------------------------------------------------------------------------------------------------------------*
#DEFINE INTERNET_INVALID_PORT_NUMBER 0
#DEFINE INTERNET_OPEN_TYPE_DIRECT 1
#DEFINE INTERNET_SERVICE_FTP 1
#DEFINE FTP_TRANSFER_TYPE_ASCII 1
#DEFINE FTP_TRANSFER_TYPE_BINARY 2
#DEFINE INTERNET_FLAG_TRANSFER_BINARY 2147483648 && &H80000000
#DEFINE INTERNET_FLAG_NEED_FILE 16
#DEFINE INTERNET_FLAG_RELOAD 2147483648 && &H80000000
#DEFINE FILE_ATTRIBUTE_DIRECTORY 16
#DEFINE GENERIC_READ 2147483648 && &H80000000
#DEFINE GENERIC_WRITE 1073741824 && &H40000000
*!* DO FORM ATTENTEMISEAJOUR WITH "Emission du fichier "+p_fichier_local+"
vers " +ALLTRIM(p_connexion)+"/"+p_fichier_serveur
p_ftp.mConnect = InternetConnect (p_ftp.mOpen, p_connexion,;
INTERNET_INVALID_PORT_NUMBER,;
p_login, p_motpasse, INTERNET_SERVICE_FTP, 0, 0)
IF p_ftp.mConnect = 0
m.lRetour= .F.
ELSE
m.lRetour=p_ftp.changerepertoire(p_chemin_serveur)
IF !m.lRetour
m.lRetour=p_ftp.changerepertoire('/')
m.lRetour=p_ftp.changerepertoire(p_chemin_serveur)
ENDIF
IF !EMPTY(m.lRetour)
* Raz du flag lRetour
m.lRetour=.F.
m.hFTP=p_ftp.OuvrirFichier(p_fichier_serveur,.T.)
IF m.hFTP>0
m.lRetour= p_ftp.maj_ftp_serveur(p_fichier_local, p_fichier_serveur, m.hFTP)
IF m.lRetour
*!* ATTENTEMISEAJOUR.INFORMATION("TRANSFERT REUSSI..")
WAIT "TRANSFERT REUSSI" WINDOWS TIMEOUT 2
ELSE
*!* ATTENTEMISEAJOUR.INFORMATION("ECHEC TRANSFERT..")
WAIT "ECHEC TRANSFERT" WINDOWS TIMEOUT 2
ENDIF
ELSE
*!* ATTENTEMISEAJOUR.INFORMATION("ECHEC TRANSFERT..")
WAIT "ECHEC ACCES TRANSFERT FTP" WINDOWS TIMEOUT 2
ENDIF
ENDIF
ENDIF
InternetCloseHandle(p_ftp.mConnect)
*!* ATTENTEMISEAJOUR.RELEASE()
RETURN m.lRetour
ENDFUNC
** Classe de gestion des accès FTP développée par ANATOLE d'AtoutFox
DEFINE CLASS FTP AS CUSTOM
mOpen = NULL && handle de l'ouverture internet
mConnect = NULL && handle de connexion au serveur FTP
*--------------------------------------------------------------------------*
PROCEDURE INIT()
DECLARE INTEGER InternetOpen IN wininet;
STRING sAgent,;
INTEGER lAccessType,;
STRING sProxyName,;
STRING sProxyBypass,;
STRING lFlags
DECLARE INTEGER InternetCloseHandle IN wininet;
INTEGER hInet
DECLARE INTEGER InternetConnect IN wininet;
INTEGER hInternetSession,;
STRING sServerName,;
INTEGER nServerPort,;
STRING sUsername,;
STRING sPassword,;
INTEGER lService,;
INTEGER lFlags,;
INTEGER lContext
DECLARE INTEGER FtpFindFirstFile IN wininet;
INTEGER hFtpSession,;
STRING lpszSearchFile,;
STRING @lpFindFileData,;
INTEGER dwFlags,;
INTEGER dwContent
DECLARE INTEGER InternetFindNextFile IN wininet;
INTEGER hFind,;
STRING @lpvFindData
DECLARE INTEGER FtpGetCurrentDirectory IN wininet;
INTEGER hFtpSession,;
STRING @lpszDirectory,;
INTEGER @lpdwCurrentDirectory
DECLARE INTEGER FtpSetCurrentDirectory IN wininet;
INTEGER hFtpSession,;
STRING @lpszDirectory
DECLARE INTEGER FtpOpenFile IN wininet;
INTEGER hFtpSession,;
STRING sFileName,;
INTEGER lAccess,;
INTEGER lFlags,;
INTEGER lContext
DECLARE INTEGER InternetReadFile IN wininet;
INTEGER hFile,;
STRING @lpBuffer,;
INTEGER dwNumberOfBytesToRead,;
INTEGER @lpdwNumberOfBytesRead
DECLARE INTEGER InternetWriteFile IN wininet;
INTEGER hFile,;
STRING @lpBuffer,;
INTEGER dwNumberOfBytesToWrite,;
INTEGER @lpdwNumberOfBytesWritten
DECLARE INTEGER FtpGetFile IN wininet;
INTEGER hFtpSession,;
STRING lpszRemoteFile,;
STRING lpszNewFile,;
INTEGER fFailIfExists,;
INTEGER dwFlagsAndAttributes,;
INTEGER dwFlags,;
INTEGER dwContext
DECLARE INTEGER FtpPutFile IN wininet;
INTEGER hConnect,;
STRING lpszLocalFile,;
STRING lpszNewRemoteFile,;
INTEGER dwFlags,;
INTEGER dwContext
DECLARE INTEGER FtpDeleteFile IN wininet;
INTEGER hConnect,;
STRING lpszFileName
DECLARE INTEGER FtpCreateDirectory IN wininet;
INTEGER hFtpSession,;
STRING lpszDirectory
DECLARE INTEGER FtpRemoveDirectory IN wininet;
INTEGER hFtpSession,;
STRING lpszDirectory
DECLARE INTEGER FtpGetFileSize IN wininet;
INTEGER hFile,;
INTEGER @ lpdwFileSizeHigh
DECLARE INTEGER FtpRenameFile IN wininet;
INTEGER hFtpSession,;
STRING lpdzExisting,;
STRING lpdzNew
DECLARE INTEGER FileTimeToSystemTime IN kernel32;
STRING @lpFileTime,;
STRING @lpSystemTime
ENDPROC && init
*--------------------------------------------------------------------------*
*---> Se connecte a internet
FUNCTION CONNEXION(p_strHost, p_strUser, p_strPwd)
LOCAL iDebut, lRetour
m.iDebut=SECONDS()
DO WHILE SECONDS()<m.iDebut+_ftptimeout
WITH THIS
..mOpen = InternetOpen (program(0), INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0)
IF .mOpen = 0
m.lRetour= .F.
ELSE
m.lRetour= .T.
EXIT
ENDIF
ENDWITH
DOEVENTS
ENDDO
RETURN m.lRetour
ENDFUNC
*--------------------------------------------------------------------------*
*---> Fermeture de la connexion à la destruction de l'objet
PROCEDURE DESTROY()
InternetCloseHandle(THIS.mOpen)
ENDPROC && destroy
*--------------------------------------------------------------------------*
*---> Renvoie le répertoire courant
FUNCTION RepertoireCourant()
LOCAL cRepertoire, iLong
m.cRepertoire = SPACE(250)
m.iLong = LEN(m.cRepertoire)
IF FtpGetCurrentDirectory (THIS.mConnect, @cRepertoire, @iLong) = 1
RETURN LEFT(m.cRepertoire, iLong)
ELSE
RETURN ""
ENDIF
ENDFUNC
*--------------------------------------------------------------------------*
PROCEDURE changerepertoire(p_dir )
RETURN FtpSetCurrentDirectory(THIS.mConnect, p_dir) >0
ENDPROC && ChangeRepertoire
*--------------------------------------------------------------------------*
*---> Envoie un fichier sur le serveur
FUNCTION EnvoyerFichier(p_local, p_remote)
RETURN FtpPutFile(THIS.mConnect, p_local, p_remote,
FTP_TRANSFER_TYPE_BINARY, 0) > 0
ENDFUNC
*--------------------------------------------------------------------------*
*---> Télécharge un fichier depuis le serveur
FUNCTION prendrefichier(p_remote, p_local)
RETURN FtpGetFile(THIS.mConnect ,;
p_remote ,;
p_local ,;
1 ,; && échec si existe en local
FILE_ATTRIBUTE_DIRECTORY ,;
FTP_TRANSFER_TYPE_BINARY ,;
0) > 0
ENDFUNC
*------------------------------------------------------------------------*
FUNCTION maj_ftp_local (p_serveur, p_local, p_h, p_taille )
LOCAL hFile, lSuccess, cBuffer, iBytesRead, iCount, cLue, lReturn, cMessage
IF FILE(p_local) && Le fichier existe-t-il?
m.hFile = FOPEN(p_local,12) && Si oui, ouvrir en lecture/écriture
ELSE
m.hFile = FCREATE(p_local) && Si non, le créer
ENDIF
IF m.hFile < 0
=MESSAGEBOX("Impossible d'ouvrir ou de créer le fichier de
sortie",16,'ERREUR',5000)
ELSE && S'il n'y a pas d'erreur, écrire dans le fichier
#DEFINE FTPBUFSIZ_GET (32*1024) && ou 4 * 1024, comme tu veux
m.lSuccess = .T.
m.cBuffer = SPACE(FTPBUFSIZ_GET)
m.iBytesRead = 0
m.iCount=0
DO WHILE m.lSuccess
m.cLue = InternetReadFile(p_h, @cBuffer, FTPBUFSIZ_GET, @iBytesRead)
DO CASE
CASE EMPTY(m.cLue)
ASSERT .F.
m.lSuccess = .F.
CASE EMPTY(m.iBytesRead)
EXIT
OTHERWISE
m.iCount = m.iCount + m.iBytesRead
ENDCASE
*!* ATTENTEMISEAJOUR.echelle(m.iCount,p_taille)
WAIT alltrim(str(m.iCount))+" sur "+alltrim(str(p_taille)+" effectué"
windows nowait
m.lReturn=.T.
DO CASE
CASE !m.lSuccess
CASE FWRITE(m.hFile , m.cBuffer, m.iBytesRead) <> m.iBytesRead
m.cMessage="ERREUR d'écriture en local"
=MESSAGEBOX(m.cMessage,16,'ERREUR',5000)
m.lSuccess = .F.
ENDCASE
ENDDO
ENDIF
=FCLOSE(m.hFile )
RETURN m.lReturn
ENDFUNC
*-------------------------------------------------------------
FUNCTION maj_ftp_serveur (p_local, p_serveur, p_h)
LOCAL hFile, nTaille, cChaine, iBytesWritten, iBytesRead, lSuccess, iCount,
cBuffer, cMessage
IF FILE(p_local)
m.hFile = FOPEN(p_local)
* Recherche la fin du fichier pour déterminer le nombre d'octets contenu
dans le fichier
m.nTaille = FSEEK(m.hFile, 0, 2) && Déplace le pointeur à EOF
IF m.nTaille <= 0
* Si le fichier est vide, affiche un message d'erreur
WAIT WINDOW "Ce fichier est vide!" NOWAIT
ELSE
* Si le fichier n'est pas vide, le programme stocke son contenu
* en mémoire, puis affiche le texte dans la fenêtre principale de Visual
FoxPro
= FSEEK(m.hFile, 0, 0)
m.cChaine = FREAD(m.hFile, m.nTaille)
ENDIF
= FCLOSE(m.hFile)
*----
#DEFINE FTPBUFSIZ_PUT (32*1024)
m.hFile = FOPEN( p_local)
m.iBytesWritten = 0
m.lSuccess = .T.
m.iBytesRead = 0
m.iCount=0
DO WHILE !FEOF(m.hFile) AND m.lSuccess
m.cBuffer = FREAD(m.hFile, FTPBUFSIZ_PUT)
DO CASE
CASE EMPTY(InternetWriteFile(p_h, @cBuffer, LEN(m.cBuffer), @iBytesWritten))
m.cMessage="Ecriture impossible sur le SERVEUR FTP"
=MESSAGEBOX(m.cMessage,16,'ERREUR',5000)
ASSERT .F.
m.lSuccess = .F.
CASE m.iBytesWritten <> LEN(m.cBuffer)
m.cMessage="Incohérence d'écriture sur le SERVEUR FTP"
=MESSAGEBOX(m.cMessage,16,'ERREUR',5000)
ASSERT .F.
m.lSuccess = .F.
OTHERWISE
m.iCount = m.iCount + m.iBytesWritten
*ATTENTEMISEAJOUR.echelle(m.iCount,m.nTaille)
WAIT alltrim(str(m.iCount))+" sur "+alltrim(str(p_taille)+" effectué"
windows nowait
ENDCASE
ENDDO
= EMPTY(p_h) OR !EMPTY(InternetCloseHandle(p_h))
= (m.hFile < 0) OR FCLOSE(m.hFile)
ELSE
m.cMessage="Fichier "+p_local+" absent.."
=MESSAGEBOX(m.cMessage,16,'ERREUR',5000)
ENDIF
RETURN m.lSuccess
ENDFUNC
*--------------------------------------------------------------------------*
*---> Ouvre un fichier sur le serveur en renvoie un pointeur
FUNCTION OuvrirFichier(p_serveur, p_Ecrire )
LOCAL iFLAGS
m.iFLAGS = IIF(p_Ecrire, GENERIC_WRITE, GENERIC_READ)
* Ajoute du forcage de la lecture hors du cache FTP
RETURN FtpOpenFile(THIS.mConnect, p_serveur, m.iFLAGS,
FTP_TRANSFER_TYPE_BINARY+INTERNET_FLAG_RELOAD, 0)
*!* Original : &&RETURN FtpOpenFile(THIS.mConnect, p_serveur, m.iFLAGS,
FTP_TRANSFER_TYPE_BINARY, 0)
ENDFUNC
*--------------------------------------------------------------------------*
*---> Retourne la taille en octet d'un fichier sur le serveur
FUNCTION TailleFichier(p_serveur)
LOCAL hFile
m.hFile = THIS.OuvrirFichier(p_serveur)
RETURN FtpGetFileSize(m.hFile, .F.)
ENDFUNC
*--------------------------------------------------------------------------*
*---> Créer un répertoire sur le serveur
FUNCTION creerrepertoire(p_dir )
RETURN FtpCreateDirectory(THIS.mConnect, p_dir)
ENDFUNC
*--------------------------------------------------------------------------*
*---> Supprime un fichier sur le serveur
FUNCTION SupprimerFichier(p_serveur)
RETURN FtpDeleteFile(THIS.mConnect, p_serveur) > 0
ENDFUNC
*--------------------------------------------------------------------------*
*---> Supprime un répertoire sur le serveur
FUNCTION SupprimerRepertoire(p_dir )
RETURN FtpRemoveDirectory(THIS.mConnect, p_dir) > 0
ENDFUNC
*--------------------------------------------------------------------------*
*---> Renomme un fichier sur le serveur
FUNCTION RenommerFichier(p_old, p_new)
RETURN FtpRenameFile(THIS.mConnect, p_old, p_new) > 0
ENDFUNC
*--------------------------------------------------------------------------*
*---> Renvoie .T. si le fichier existe sur le serveur
FUNCTION EstFichier(p_serveur)
RETURN THIS.OuvrirFichier(p_serveur) > 0
ENDFUNC
*--------------------------------------------------------------------------*
*---> Extrait la liste des objets dans le masque
* ex de masque : /*.*
*
*---> Colonnes
* 1 : nom du fichier string
* 2 : taille integer
* 3 : dernière modif. datetime
* 4 : attributs integer
*
*---> Attributs :
* 0 Normal Fichier normal. Aucun attribut n'est défini.
* 1 ReadOnly Fichier en lecture seule. L'attribut est lecture/écriture.
* 2 Hidden Fichier caché. L'attribut est lecture/écriture.
* 4 System Fichier système. L'attribut est lecture/écriture.
* 8 Volume Étiquette de volume de lecteur de disque. L'attribut est
lecture seule.
* 16 Directory Dossier ou répertoire. L'attribut est lecture seule.
* 32 Archive Le fichier a été modifié depuis la dernière sauvegarde.
L'attribut est lecture/écriture.
* 64 Alias Lien ou raccourci. L'attribut est lecture seule.
* 128 Compressed Fichier compressé. L'attribut est lecture seule.
* p_t est passé en référence (@) et mis a jour par la fonction
FUNCTION Fichiers2Array(p_masque , p_t)
LOCAL iCount, iTrouve, cTrame
WITH THIS
m.iCount = 0
m.cTrame = REPLICATE(CHR(0), 320)
m.iTrouve = FtpFindFirstFile(.mConnect, p_masque, @cTrame,
INTERNET_FLAG_NEED_FILE, 0)
IF m.iTrouve > 0
DO WHILE .T.
m.iCount = m.iCount + 1
DIMENSION p_t(m.iCount, 4)
p_t[m.iCount, 1] = LTRIM(SUBSTR(m.cTrame, 45, 250))
IF AT(CHR(0), p_t[m.iCount, 1]) <> 0
p_t[m.iCount, 1] = SUBSTR(p_t[m.iCount, 1], 1, AT(CHR(0), p_t[m.iCount,
1])-1)
ENDIF
p_t[m.iCount, 2] = .buf2num(m.cTrame, 32, 4)
p_t[m.iCount, 3] = .ftime2dtime(SUBSTR(m.cTrame, 21, 8))
p_t[m.iCount, 4] = .buf2num(m.cTrame, 0, 4)
m.cTrame = REPLICATE(CHR(0), 320)
IF InternetFindNextFile (m.iTrouve, @cTrame) <> 1
EXIT
ENDIF
ENDDO
ENDIF
InternetCloseHandle(m.iTrouve)
RETURN m.iCount
ENDWITH
ENDFUNC
*--------------------------------------------------------------------------*
HIDDEN PROCEDURE buf2num(lcBuffer, lnOffset, lnBytes)
LOCAL ii
lnResult = 0
FOR ii=1 TO lnBytes
lnResult = lnResult +;
BITLSHIFT(ASC(SUBSTR (lcBuffer, lnOffset+ii, 1)), (ii-1)*8)
ENDFOR
RETURN lnResult
ENDPROC && bug2num
*--------------------------------------------------------------------------*
HIDDEN PROCEDURE ftime2dtime(lcFileTime)
LOCAL lcSystemTime, ltResult, lcDate, lcTime, wYear, wMonth, wDay, wHour,
wMinute, wSecond, lcStoredSet
lcSystemTime = REPLI (CHR(0), 16)
= FileTimeToSystemTime (@lcFileTime, @lcSystemTime)
wYear = .buf2num(lcSystemTime, 0, 2)
wMonth = .
Journal

GLS pense que ce message est la bonne réponse ou qu'il est utile
Permalink : http://www.atoutfox.org/nntp.asp?ID=0000009041