ATOUTFOX
COMMUNAUTÉ FRANCOPHONE DES PROFESSIONNELS FOXPRO
Visual FoxPro : le développement durable

Déterminer la taille d'un fichier de téléchargement   



L'auteur

ybenam
Algérie Algérie
Membre Simple
# 0000002080
enregistré le 21/04/2008


Fiche personnelle


Note des membres
pas de note

Contributions > 05 - API et appels systèmes

Déterminer la taille d'un fichier de téléchargement
# 0000000611
ajouté le 09/06/2008 02:42:07 et modifié le 09/06/2008
consulté 8609 fois
Niveau initié

Version(s) Foxpro :
VFP 6.0

Description
Pour suivre un téléchargement à partir de 'progressbar' ou autres visualisations ,on a besoin de connaitre la taille du ficher à télécharger à l'avance. Cela est obtenu avec des appels API windows détaillées dans le programme. http://msdn.microsoft.com/en-us/library/ms906347.aspx..... pour toute info supplémentaire sur HttpQueryInfo.
Code source :
*Retourner la taille d'un fichier quelconque à télécharger en octets
yUrl="http://www.algerie-ancienne.com/livres/Sahara/1849egc.zip"  &&URL de téléchargement dont on veut déterminer la taille


clea
*Déclarations des Constantes
#DEFINE INTERNET_OPEN_TYPE_PRECONFIG  0
#DEFINE INTERNET_FLAG_RELOAD     0x80000000
#define HTTP_QUERY_CONTENT_LENGTH     5


do yDECLARATIONS        &&Charger en mémoire les déclarations des API
*******************
yhRequest  = 0
yHandle = InternetOpen("VISUAL FOXPRO", INTERNET_OPEN_TYPE_PRECONFIG,0, 0, 0)
If yHandle # 0
  yhRequest = InternetOpenUrl(yHandle, yURL, '', 0, INTERNET_FLAG_RELOAD, 0 )
endI
if yhRequest=0   &&Echec connection
  =InternetCloseHandle(yHandle)
endi
************************************************************
cBuf = REPLICATE(' ',40)
nBufLen = 40
nVoid  = 0
nQryRet = HttpQueryInfo(yhRequest, HTTP_QUERY_CONTENT_LENGTH,  @cBuf, @nBufLen, @nVoid )
IF nQryRet=1
  nSize = VAL(cBuf)
messagebox("Taille de ["+yUrl+"] ="+allt(str(nSize))+ " Octets")
ENDIF

=InternetCloseHandle(yHandle)

return

************************************************************

procedure yDECLARATIONS

Declare integer InternetOpen in wininet;
  string  sAgent,;
    integer lAccessType,;
    string sProxyName,;
    string sProxyBypass,;
    string lFlags

*lpszAgent [in] Pointer to a string variable that contains the name of the application or entity calling the Internet functions.
*dwAccessType [in] Type of access required
*lpszProxyName[in] Pointer to a string variable that contains the name of the proxy server(s)
*lpszProxyBypass[in] Pointer to a string variable that contains an optional list of host names or IP addresses, or both, that should not be routed through the proxy when dwAccessType is set to INTERNET_OPEN_TYPE_PROXY
*dwFlags[in] Unsigned long integer value that contains the flags that indicate various options affecting the behavior of the function
*Return value: a valid handle that the application passes to subsequent Win32 Internet functions. If InternetOpen fails, it returns NULL
*On exit do not forget to call the closing function -- InternetCloseHandle.
*****************************
DECLARE INTEGER InternetOpenUrl IN wininet;
    INTEGER hInternet,;
    STRING   lpszUrl,;
    STRING   lpszHeaders,;
    INTEGER dwHeadersLength,;
    INTEGER dwFlags,;
    INTEGER dwContext
*hInternet [in] HINTERNET handle to the current Internet session. The handle must have been returned by a previous call to InternetOpen.
*lpszUrl [in] Pointer to a string variable that contains the URL to begin reading.
*lpszHeaders [in] Pointer to a string variable that contains the headers to be sent to the HTTP server.
*dwHeadersLength [in] Unsigned long integer value that contains the length, in TCHARs, of the additional headers.
*dwFlags [in] Unsigned long integer value that contains the API flags.
*dwContext [in] Pointer to an unsigned long integer value that contains the application-defined value.
 *Returns a valid handle to the FTP, Gopher, or HTTP URL if the connection is successfully established, or NULL if the connection fails.
*********************************
DECLARE INTEGER InternetCloseHandle IN wininet  INTEGER hInet
* hInternet [in] Valid HINTERNET handle to be closed
 *Returns TRUE if the handle is successfully closed, or FALSE otherwise
***********************************


DECLARE INTEGER HttpQueryInfo IN wininet;
  INTEGER  hRequest,;
  LONG     dwInfoLevel,;
  STRING @ lpvBuffer,;
  LONG   @ lpdwBufferLength,;
  LONG   @ lpdwIndex
*Retrieves header information associated with an HTTP request.
*hRequest [in] Handle returned by HttpOpenRequest or InternetOpenUrl.
*dwInfoLevel [in] Combination of an attribute to be retrieved and flags that modify the request. For a list of possible attribute and modifier values, see Query Info Flags.
*lpvBuffer [in] Pointer to a buffer that receives the information. This parameter must not be NULL.
*lpdwBufferLength [in] Pointer to a variable that contains the size of the data buffer, in bytes.
*lpdwIndex [in, out] Pointer to a zero-based header index used to enumerate multiple headers with the same name.

*Source API http://www.news2news.com/vfp/ (Merci - PI les exemples sont payants sur ce site !)
Commentaires
le 06/07/2008, Gregory Adam a écrit :
- Tu oublies de fermer yhRequest

- J'aurais mis: nSize = VAL(left(cBuf, m.nBufLen ))

- Si tu ajoutes HTTP_QUERY_FLAG_NUMBER a HttpQueryInfo()
nBufLen sera 4
et
nSize = ctobin(left(cBuf, m.nBufLen), '4rs')


le 06/07/2008, ybenam a écrit :
Merci de ce feed back .Oui effectivement il faut ajouter
=InternetCloseHandle(yhRequest)
au niveau de
=InternetCloseHandle(yHandle)

Pour ctobin(left(cBuf, m.nBufLen), '4rs') : cette fonction n'existe pas en VFP6.0 ou du moins elle ne fait pas la même syntaxe et transcrite ainsi elle renvoie une erreur.Votre syntaxe est valable pour les versions récentes>=7(A vérifier).
CTOBIN(cExpression) tout court en vfp6.0 n'admet pas de second paramètre.


Le programme a été testé sur plusieurs cibles et marche très bien !


Publicité

Les pubs en cours :

www.atoutfox.org - Site de la Communauté Francophone des Professionnels FoxPro - v3.4.0 - © 2004-2024.
Cette page est générée par un composant COM+ développé en Visual FoxPro 9.0-SP2-HF3