FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Downloading a file from internet
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Downloading a file from internet
Posted: Fri Sep 20, 2013 07:46 AM
Hello,

I use this code to download a file from internet :
Code (fw): Select all Collapse
MsgRun("Downloading ...","One moment...",{||IF(URLDownloadToFile(0,"http://www.ma-consult.be/test.txt","..\test.txt",0,0 ) == 0,UpdDown:=0,UpdDown:=1)})
This code is running just fine, apart from one problem.

If this download is done twice in a very short time, it is considered that the file is the same. So probably the file is copied from the tempory internet files. If the file on internet has been changed meanwhile, the new file isn't downloaded.

How can I force my application to download the file from the internet each time it is asked for, also in case the download is done more than once in a short time?

I hope the readers will understand what I mean.

Thanks a lot in advance.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 99
Joined: Thu Jul 12, 2007 02:02 PM
Re: Downloading a file from internet
Posted: Fri Sep 20, 2013 09:05 AM
Hello,

You can try this function I wrote to remove the files from the cache
before downloading the file

Code (fw): Select all Collapse
        aFiles := { "http://www.example.it/file1.exe", "http://www.example.it/file2.exe" }

        DELFILECACHE( aFiles )

        


#pragma BEGINDUMP

#include <stdio.h>
#include <WinTen.h>
#include <Windows.h>
#include <ClipApi.h>
#include <WinINet.h>
#include "hbapi.h"
#include "hbapiitm.h"
#include "hbapifs.h"
#include "hbvm.h"
#include "hbstack.h"

static mystrcmp( char *val1, char *val2 )

{
        int     nret = 0 ;

        for ( ; *val1 ; val1++, val2++ )  {
                if ( ! *val2 || *val1 != *val2 )  {
                        nret = 1 ;
                        break ;
                }
        }
        if ( ! nret && *val2 )
                nret = 1 ;

        return nret ;
}

HB_FUNC( DELFILECACHE )

{
        BOOL                            bResult = FALSE ;
        BOOL                            bDone = FALSE ;
        LPINTERNET_CACHE_ENTRY_INFO     lpCacheEntry = NULL ;  
        DWORD                           dwTrySize, dwEntrySize = 4096 ; // start buffer size    
        HANDLE                          hCacheDir = NULL ;    
        DWORD                           dwError = ERROR_INSUFFICIENT_BUFFER ;
        BOOL                            bSuccess ;
        ITEM                            iFiles, iElem ;
        int                             nfile, npos ;
        char                            buffile[FILENAME_MAX] ;
        
        iFiles = hb_itemParam( 1 ) ;
        nfile = _parinfa( 1, 0 ) ;
    
        do  {
                switch ( dwError )  {
                        // need a bigger buffer
                        case ERROR_INSUFFICIENT_BUFFER : 
                                if ( lpCacheEntry != NULL )
                                        free( (LPBYTE)lpCacheEntry ) ;
                                lpCacheEntry = (LPINTERNET_CACHE_ENTRY_INFO) malloc( dwEntrySize ) ;        
                                lpCacheEntry->dwStructSize = dwEntrySize ;
                                dwTrySize = dwEntrySize ;
                                
                                if ( hCacheDir == NULL )                
                                        bSuccess = (hCacheDir = FindFirstUrlCacheEntry( NULL, lpCacheEntry, &dwTrySize )) != NULL ;
                                else
                                        bSuccess = FindNextUrlCacheEntry(hCacheDir, lpCacheEntry, &dwTrySize ) ;
                                if ( bSuccess )
                                        dwError = ERROR_SUCCESS ;    
                                else  {
                                        dwError = GetLastError() ;
                                        dwEntrySize = dwTrySize ; // use new size returned
                                }
                                break ;
                                
                        // we are done
                        case ERROR_NO_MORE_ITEMS :
                                bDone = TRUE ;
                                bResult = TRUE ;                
                                break ;
                                
                        // we have got an entry
                        case ERROR_SUCCESS:                       
                                // don't delete cookie entry

                                if ( !( lpCacheEntry->CacheEntryType & COOKIE_CACHE_ENTRY ) )  {
                                        for ( npos = 1 ; npos <= nfile ; npos++ )  {
                                                iElem = hb_itemArrayGet( iFiles, npos ) ;
                
                                                hb_itemCopyC( iElem, buffile, 0 ) ;
                                                buffile[hb_itemSize( iElem )] = '\0' ;
                                        
                                                if ( ! mystrcmp( (char *)(lpCacheEntry->lpszSourceUrlName), buffile ) )  {
                                                        DeleteUrlCacheEntry( lpCacheEntry->lpszSourceUrlName ) ;
                                                        break ;
                                                }        
                                        }
                                }        
        
                                // get ready for next entry
                                dwTrySize = dwEntrySize; 
                                if ( FindNextUrlCacheEntry(hCacheDir, lpCacheEntry, &dwTrySize ) )
                                        dwError = ERROR_SUCCESS ;
                                else  {
                                        dwError = GetLastError() ;
                                        dwEntrySize = dwTrySize ; // use new size returned
                                }
                                break;
                                
                        // unknown error
                        default:
                                bDone = TRUE ;
                                break ;
                }
                if ( bDone )  {
                        free( (LPBYTE)lpCacheEntry ) ;
                        if ( hCacheDir )
                                FindCloseUrlCache( hCacheDir ) ;
                }
        }  while ( !bDone ) ;
            
        _retnl( bResult ) ;
}

#pragma ENDDUMP


regards
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Re: Downloading a file from internet - solved
Posted: Fri Sep 20, 2013 10:13 AM

Hello MaxP,

Thank you very much for your help.

It is working great now.

Have a nice weekend.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Continue the discussion