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

From: Steven Burn (pvt_at_noyb.com)
Date: 06/28/04


Date: Tue, 29 Jun 2004 00:06:33 +0100

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: Code for controling FTP from Access VBA
    ... cFileName As String * MAX_PATH ... Dim mHInternet As Long ... InternetCloseHandle mHFoundFile ... Public Property Let DeleteFileName ...
    (comp.databases.ms-access)
  • Re: Xfer file to AS400 FTP server w/ VB6
    ... Dim strDest As String, strSource As String, strSizeFile As String ... Dim lBytesRead As Long, nFile As Integer, strLastChunk As String ... InternetCloseHandle hFile ...
    (microsoft.public.vb.general.discussion)
  • Re: open url mit Passwort öffnen
    ... ByVal URL As String, _ ... Dim hInet As Long ... InternetCloseHandle hInet ... Passwort oder User nicht stimmen, ...
    (microsoft.public.de.vb)
  • openUrl mit Passwort öffnen
    ... Wo muss ich da Benutzer und Passwort angeben. ... ByVal URL As String, _ ... Dim hInet As Long ... InternetCloseHandle hInet ...
    (microsoft.public.de.vb)
  • Search pattern
    ... Dim strfile As String ... Dim bAddressFound As Boolean ... Dim strCurrentChar As String ...
    (comp.databases.ms-access)

Loading