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:02:24 -0400

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: How to download a file from the internet using inet control
    ... :> 'Download an URL to a local file without caching. ... :> Public Function DownloadURL2FileNoCache(ByRef sSourceUrl As String, ... :> Dim hOpenUrl As Long ... :> Public Function DownloadURL2Memory(ByRef sSourceUrl As String, ...
    (microsoft.public.vb.general.discussion)
  • Re: Help with Function - converting a double num into a hh:mm:ss format
    ... I needed this to figure out how seconds it took to download X bits ... > Function CalculateDownloadTimeAs String ... > Dim d As Double ... >> with the Excel file and typed it in. ...
    (microsoft.public.dotnet.framework.aspnet)
  • FtpGetFile
    ... I'm using VB6 to download a file from an ftp server. ... you can see I used serveral function calls from wininet.dll. ... Public Sub DownloadFile(strURL As String, strUser As String, strPassword As ... Dim szFileRemote As String, szDirRemote As String, szFileLocal As String ...
    (microsoft.public.vb.winapi)
  • =?Utf-8?Q?Loop_or_For..Next_questions=E2=80=A6?=
    ... User has to select individual month to download their data ... Dim qdf As QueryDef ... Dim strSQL As String ... DoCmd.RepaintObject acForm, "frmprocessing" ...
    (microsoft.public.access.modulesdaovba)
  • Search pattern
    ... Dim strfile As String ... Dim bAddressFound As Boolean ... Dim strCurrentChar As String ...
    (comp.databases.ms-access)