Re: How to download a file from the internet using inet control

From: Randy Birch (rgb_removethis_at_mvps.org)
Date: 06/28/04


Date: Mon, 28 Jun 2004 19:18:30 -0400

Hi Steve ...

It's not mine ... someone flipped it to me claiming it was an alternate to
my UrlDownloadToFile/DeleteUrlCacheEntry to bypass the cache. I've actually
not tried it, but I suspect it is representative of how much more involved
using those APIs over UrlDownloadToFile directly.

Is it yours?

-- 
Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.
"Steven Burn" <pvt@noyb.com> wrote in message 
news:O5OYATWXEHA.712@TK2MSFTNGP11.phx.gbl...
: oooo...... now that is some beautiful coding Randy ;o)
:
: --
:
: Regards
:
: Steven Burn
: Ur I.T. Mate Group
: www.it-mate.co.uk
:
: Keeping it FREE!
:
:
: "Randy Birch" <rgb_removethis@mvps.org> wrote in message
: news:uMCH7PWXEHA.3668@TK2MSFTNGP09.phx.gbl...
: > This downloads to a disk file which you can read into a string variable 
in
: > one blast ....
: >
: >    hFile = FreeFile
: >    Open sLocalFilename For Input As #hFile
: >       somestring = Input$(LOF(hFile), hFile)
: >    Close #hFile
: >    Kill sLocalFilename
: >
: > ... and delete the downloaded file using Kill.  You could also use this
: > version ...
: http://vbnet.mvps.org/code/internet/urldownloadtofilenocache.htm
: > ... and call DeleteUrlCacheEntry after the download if you wanted to 
nuke
: > the cached copy as well.
: >
: > There is another way, posted below, using the Internet APIs, but as 
you'll
: > see it is a far more involved process to obtain the same goal ...
: >
: > 'Download an URL to a local file without caching.
: > Public Function DownloadURL2FileNoCache(ByRef sSourceUrl As String, 
ByRef
: > sLocalFile As String) As Boolean
: > Const BufSize As Long = 131072
: > Dim Buffer(0 To BufSize - 1) As Byte
: > Dim hOpen As Long
: > Dim hOpenUrl As Long
: > Dim fHandle As Long
: > Dim lNumberOfBytesRead As Long
: > Dim lNumberOfBytesWritten As Long
: >
: >     hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG,
: > vbNullString, vbNullString, 0)
: >     hOpenUrl = InternetOpenUrl(hOpen, sSourceUrl, vbNullString, 0,
: > INTERNET_FLAG_RELOAD + INTERNET_FLAG_NO_CACHE_WRITE, 0)
: >     fHandle = CreateFile(sLocalFile, GENERIC_WRITE, 0, 0, CREATE_ALWAYS,
: > FILE_ATTRIBUTE_NORMAL, 0)
: >
: >     Do
: >         Call InternetReadFile(hOpenUrl, Buffer(0), BufSize,
: > lNumberOfBytesRead)
: >         If lNumberOfBytesRead = 0 Then
: >             'When bytes are written, return True
: >             DownloadURL2FileNoCache = lNumberOfBytesWritten > 0
: >             Exit Do
: >         Else
: >             If WriteFile(fHandle, Buffer(0), lNumberOfBytesRead,
: > lNumberOfBytesWritten, 0&) = 0 Then
: >                 DownloadURL2FileNoCache = False
: >                 Exit Do
: >             End If
: >             If lNumberOfBytesRead < BufSize Then
: >                 DownloadURL2FileNoCache = True
: >                 Exit Do
: >             End If
: >         End If
: >     Loop
: >
: >     'Close File en Internet Handles
: >     If fHandle <> 0 Then CloseHandle fHandle
: >     If hOpenUrl <> 0 Then InternetCloseHandle hOpenUrl
: >     If hOpen <> 0 Then InternetCloseHandle hOpen
: > End Function
: >
: > 'Download an URL to a byte array.
: > Public Function DownloadURL2Memory(ByRef sSourceUrl As String, ByRef
: > Buffer() As Byte) As Boolean
: > Const SubBufSize As Long = 65536
: > Dim hOpen As Long
: > Dim hOpenUrl As Long
: > Dim iSubBufOffset As Long
: > Dim lNumberOfBytesRead  As Long
: >
: >     hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG,
: > vbNullString, vbNullString, 0)
: >     hOpenUrl = InternetOpenUrl(hOpen, sSourceUrl, vbNullString, 0,
: > INTERNET_FLAG_RELOAD + INTERNET_FLAG_NO_CACHE_WRITE, 0)
: >
: >     iSubBufOffset = 0
: >     ReDim Buffer(iSubBufOffset To SubBufSize - 1) As Byte
: >     Do
: >         Call InternetReadFile(hOpenUrl, Buffer(iSubBufOffset), 
SubBufSize,
: > lNumberOfBytesRead)
: >         If lNumberOfBytesRead < SubBufSize Then
: >             If iSubBufOffset + lNumberOfBytesRead = 0 Then
: >                 DownloadURL2Memory = False
: >             Else
: >                 ReDim Preserve Buffer(0 To iSubBufOffset +
: > lNumberOfBytesRead - 1) As Byte
: >                 DownloadURL2Memory = True
: >             End If
: >             Exit Do
: >         Else
: >             iSubBufOffset = iSubBufOffset + lNumberOfBytesRead
: >             ReDim Preserve Buffer(0 To iSubBufOffset + SubBufSize - 1) 
As
: > Byte
: >         End If
: >     Loop
: >
: >     'Close Internet Handles
: >     If hOpenUrl <> 0 Then InternetCloseHandle hOpenUrl
: >     If hOpen <> 0 Then InternetCloseHandle hOpen
: > End Function
: >
: > 'Download an URL in a string
: > Public Function DownloadURL2String(ByRef sSourceUrl As String, ByRef
: sString
: > As String, Optional SendAsUnicode As Boolean = False) As Boolean
: > Dim Buffer() As Byte
: >
: >     DownloadURL2String = DownloadURL2Memory(sSourceUrl, Buffer)
: >     If DownloadURL2String Then
: >         If SendAsUnicode Then
: >             sString = Buffer
: >         Else
: >             sString = StrConv(Buffer, vbUnicode)
: >         End If
: >     End If
: > End Function
: >
: >
: >
: > --
: >
: > Randy Birch
: > MVP Visual Basic
: > http://vbnet.mvps.org/
: > Please respond only to the newsgroups so all can benefit.
: >
: >
: > "Alan Silver" <alan-silver@nospam.thanx> wrote in message
: > news:HMP6mMQepF4AFwKs@nospamthankyou.spam...
: > : >>Perhaps this will help...
: > : >>
: > : >>http://vbnet.mvps.org/index.html?code/internet/urldownloadtofile.htm
: > : >
: > : >What if you want to download to a String variable ? I mean to grab a
: > : >web page, but store the HTML in a String rather than in a file. I
: > : >couldn't see an API to do that.
: > :
: > : Posted too fast ... forgot to add that I want to do this from a DLL, 
so
: > : APIs are the preferred method as I don't want to start creating 
controls
: > : inside a DLL.
: > :
: > : --
: > : Alan Silver
: > : (anything added below this line is nothing to do with me)
: >
:
: 


Relevant Pages

  • Re: Update DB table from visual basic script
    ... Dim dbs As Database ... Public Function UpdateDB(dbName As String) As Boolean ... Public Function updateDB_fnameAs Boolean ... On Error Resume Next ' if proc does not exist then return blank string ...
    (comp.databases.ms-access)
  • Load registry hive (AdjustTokenPrivileges error)
    ... Public PrivilegeCount As Int32 ... Public Function RegLoadKey(ByVal hKey As Int32, ... String, ByVal lpFile As String) As Int32 ... Dim strKeyName As String ...
    (microsoft.public.vb.winapi)
  • Load registry hive (AdjustTokenPrivileges error)
    ... Public PrivilegeCount As Int32 ... Public Function RegLoadKey(ByVal hKey As Int32, ... String, ByVal lpFile As String) As Int32 ... Dim strKeyName As String ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Help creating object in excel VBA
    ... Dim confirm, sent As Boolean ... Dim rowColArrayAs String ... ' TODO: Call CreateEmailMsg ... Public Function GetApptRecAs String, ...
    (microsoft.public.excel.programming)
  • Help creating object in excel VBA
    ... Outlook, and using an word.doc as the message body. ... Dim confirm, sent As Boolean ... Dim rowColArrayAs String ... Public Function GetApptRecAs String, ...
    (microsoft.public.excel.programming)