Skip to content

VFP: Webpage to file

Read web content to a file

A Visual Foxpro function to read a web page’s content into a (cached) file.

UrlToFile.prg

LPARAMETERS tcRemote
 * URL reader from Fox Wiki site: http://fox.wikis.com/wc.dll?Wiki~ReadUrl~VFP
 * Accept URL
 * Return cached filename
 * Note: file gets deleted after first contact. So do filetostr() first! After file() it will be gone.
 LOCAL lnResult, lcTargetFile
 lcTargetFile = REPLICATE(CHR(0), 250)
 DECLARE INTEGER URLDownloadToCacheFile IN urlmon;
 INTEGER lpUnkcaller, STRING szURL, STRING @szFileName,;
 INTEGER dwBufLength, INTEGER dwReserved, INTEGER pBSC
 WAIT WINDOW NOWAIT "Downloading remote file..."
 lnResult = URLDownloadToCacheFile(0, tcRemote, @lcTargetFile,LEN(lcTargetFile), 0,0)
 WAIT CLEAR
 RETURN STRTRAN(lcTargetFile, Chr(0), "")

And wrapper UrlToStr.prg:

LPARAMETERS tcRemote
 * Wrapper for UrlToFile to read web content into a string
 LOCAL lcFile,lcResult
 lcFile = UrlToFile(tcRemote)
 lcResult = FILETOSTR( lcFile)
 RETURN lcResult

Leave a Reply

Your email address will not be published.