LOCAL lcFilePathOnDesktop
lcFilePathOnDesktop = GetUsersDesktop() + "httpget.zip"
?HttpGetFile("[URL unfurl="true"]http://www.sweetpotatosoftware.com/httpget.zip",[/URL] lcFilePathOnDesktop)
****************************
Function HttpGetFile(tcUrlName, tcDestinationFile)
****************************
Declare Integer InternetOpen In wininet.Dll String sAgent, ;
INTEGER lAccessType, String sProxyName, ;
STRING sProxyBypass, Integer lFlags
Declare Integer InternetOpenUrl In wininet.Dll ;
INTEGER hInternetSession, String sUrl, String sHeaders, ;
INTEGER lHeadersLength, Integer lFlags, Integer lContext
Declare Integer InternetReadFile In wininet.Dll Integer hfile, ;
STRING @sBuffer, Integer lNumberofBytesToRead, Integer @lBytesRead
Declare short InternetCloseHandle In wininet.Dll Integer hInst
#Define INTERNET_OPEN_TYPE_PRECONFIG 0
#Define INTERNET_OPEN_TYPE_DIRECT 1
#Define INTERNET_OPEN_TYPE_PROXY 3
#Define SYNCHRONOUS 0
#Define INTERNET_FLAG_RELOAD 2147483648
#Define CR Chr(13)
* what application is using Internet services?
sAgent = "VPF 8.0"
hInternetSession = InternetOpen(sAgent, INTERNET_OPEN_TYPE_PRECONFIG, ;
'', '', SYNCHRONOUS)
* debugging line - uncomment to see session handle
* WAIT WINDOW "Internet session handle: " + LTRIM(STR(hInternetSession))
If hInternetSession = 0
Wait Window "Internet session cannot be established" Time 2
Return 0
Endif
hUrlFile = InternetOpenUrl(hInternetSession, tcUrlName, '', ;
0, INTERNET_FLAG_RELOAD, 0)
* debugging line - uncomment to see URL handle
* WAIT WINDOW "URL Handle: " + LTRIM(STR(hUrlFile))
If hUrlFile = 0
Wait Window "URL cannot be opened"
Return 0
Endif
lnFileHandle = Fcreate(tcDestinationFile)
If lnFileHandle < 0 && Check for error opening file
Wait 'Cannot open or create output file' Window Nowait
Else && If no error, write to file
On Error Do HandleError With lnFileHandle, tcDestinationFile && Just in case
lnTotalBytesWritten = 0
llFileExists = .T.
Do While .T.
* set aside a big buffer
sReadBuffer = Space(32767)
lBytesRead = 0
m.OK = InternetReadFile(hUrlFile, @sReadBuffer, ;
LEN(sReadBuffer), @lBytesRead)
* debugging code - uncomment if necessary
*WAIT WINDOW "hURLFile: " + LTRIM(STR(hURLFile)) + CR + ;
* "lBytesRead: " + LTRIM(STR(lBytesRead)) + CR ;
* + "m.OK : " + LTRIM(STR(m.OK))
If Occurs("404 NOT FOUND", sReadBuffer) > 0
llFileExists = .F.
EXIT
Endif
lnTotalBytesWritten = lnTotalBytesWritten+ Fwrite(lnFileHandle, sReadBuffer)
* error trap - either a read failure or read past eof()
If m.OK = 0 Or lBytesRead = 0
Exit
Endif
Enddo
* close all the handles we opened
=InternetCloseHandle(hUrlFile)
=InternetCloseHandle(hInternetSession)
Endif
=Fclose(lnFileHandle) && Close file
If !llFileExists
Messagebox("The file " + tcUrlName + " does not exist.",64,"UNABLE TO RETRIEVE FILE")
Erase (tcDestinationFile)
Endif
Clear Dlls InternetOpen, InternetOpenUrl, InternetReadFile, InternetCloseHandle
Return (lnTotalBytesWritten) && Total Number of Bytes Written
Endfunc
****************************
Procedure HandleError(tnFileHandle, tcFileToErase)
****************************
=Fclose(tnFileHandle) && Close file
Erase (tcFileToErase)
Messagebox("An Error has occured and this program will now shut down.",16,"ERROR RETRIEVING FILE")
If _vfp.StartMode = 0 && Running in VFP IDE
Cancel
Else
Quit
Endif
Endproc
***********************************************
*!* Not necessary for the download
*!* Only for the example so file will be on your desktop
FUNCTION GetUsersDesktop()
***********************************************
DECLARE SHORT SHGetFolderPath IN SHFolder.dll ;
INTEGER hwndOwner, INTEGER nFolder, INTEGER hToken, ;
INTEGER dwFlags, STRING @pszPath
#DEFINE CSIDL_DESKTOP 0x0000
LOCAL cFolderPath, cDesktopPath
cFolderPath = space(255)
SHGetFolderPath(0, CSIDL_DESKTOP, 0, 0, @cFolderPath)
cDesktopPath = Alltrim(cFolderPath)
cDesktopPath = SubStr(cDesktopPath,1, Len(cDesktopPath)-1)
CLEAR DLLS SHGetFolderPath
RETURN (ADDBS(cDesktopPath))
ENDFUNC