Results 1 to 16 of 16

Thread: download file from net - w/o events

  1. #1

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    download file from net - w/o events

    am looking for a really simple (and compatable) way of downloading a file from the net - i also want my app not to run the next line of code until this has happened

    i have seen api to do this before but cant seem to find any atm

    thanks in advance
    Kris Bennett

  2. #2
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: download file from net - w/o events

    Try this. You need a form with an Internet Transfer Control called inetDownload on it.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.  
    5.     DownLoadFromURL "http://www.yourwebsite.com/files/", "filetodownload.txt"
    6.     Unload Me
    7.    
    8. End Sub
    9.  
    10. Private Sub DownLoadFromURL(ByVal url As String, ByVal filename As String)
    11.     '
    12.     '   Download a file from a given URL
    13.     '
    14.     Dim bytes() As Byte
    15.     Dim fnum As Integer
    16.     Dim ftext As String
    17.     '
    18.     ' Download the file and load into byte array
    19.     '
    20.     ftext = url & filename
    21.     bytes() = inetDownload.OpenURL(ftext, icByteArray)
    22.  
    23.     fnum = FreeFile
    24.     '
    25.     '   Write the downloaded file to disk
    26.     '
    27.     Open App.Path & "\" & filename For Binary Access Write As #fnum
    28.    
    29.     Put #fnum, , bytes()
    30.     Close #fnum
    31.    
    32. End Sub
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  3. #3

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: download file from net - w/o events

    um does any one no how to do it without using 3rd party controls?

    thanks
    Kris

  4. #4
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: download file from net - w/o events

    It took about 5 seconds of searching to turn up this link....
    http://www.devx.com/vb2themax/Tip/19203

    Remember... Google is your friend
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: download file from net - w/o events

    Or you can use the URLDownloadToFile API function.
    VB Code:
    1. Private Declare Function URLDownloadToFile Lib "URLMON.dll" Alias "URLDownloadToFileA" ( _
    2.     ByVal pCaller As Long, _
    3.     ByVal szUrl As String, _
    4.     ByVal szFileName As String, _
    5.     ByVal dwFlags As Long, _
    6.     ByVal lpfnCB As Long _
    7. ) As Long
    8.  
    9. Public Sub DownloadFile(ByVal sUrl As String, ByVal sLocalFile As String)
    10.     Call URLDownloadToFile(0&, sUrl, sLocalFile, 0&, 0&)
    11. End Sub
    Simply use it like this:
    VB Code:
    1. Call DownloadFile("http://www.domain.com/theFile.zip", "c:\SomePath\theFile.zip")

  6. #6
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: download file from net - w/o events

    Quote Originally Posted by Joacim Andersson
    Or you can use the URLDownloadToFile API function
    You just need to be aware that this requires that Internet Explorer 5 or greater is installed.
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: download file from net - w/o events

    Quote Originally Posted by pnish
    You just need to be aware that this requires that Internet Explorer 5 or greater is installed.
    Is there anyone who still use IE4 or earlier

  8. #8
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: download file from net - w/o events

    The sad truth is that you never really know until your app breaks...
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  9. #9
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: download file from net - w/o events

    VB Code:
    1. Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
    2. "URLDownloadToFileA" (ByVal pCaller As Long, _
    3. ByVal szURL As String, _
    4. ByVal szFileName As String, _
    5. ByVal dwReserved As Long, _
    6. ByVal lpfnCB As Long) As Long
    7.  
    8. Private Function DownloadFile(URL As String, LocalFilename As String) As Boolean
    9.     Dim lngRetVal As Long
    10.     lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
    11.     If lngRetVal = 0 Then DownloadFile = True
    12. End Function
    13.  
    14. Private Sub Command1_Click()
    15.     If DownloadFile("http://www.mywebsite.com/somefile.exe", "c:\somefile.exe") Then
    16.         Debug.Print "Download Complete"
    17.     End If
    18. End Sub

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: download file from net - w/o events

    Quote Originally Posted by pnish
    The sad truth is that you never really know until your app breaks...
    Well, putting IE5 or later as a requirement for your application should not be a problem. Besides you can always use code like the following to test if it's installed or not before calling URLDownloadToFile
    VB Code:
    1. Private Declare Function DllGetVersion Lib "shlwapi" ( _
    2.     dwVersion As DllVersionInfo _
    3. ) As Long
    4.  
    5. Private Type DllVersionInfo
    6.     cbSize As Long
    7.     dwMajorVersion As Long
    8.     dwMinorVersion As Long
    9.     dwBuildNumber As Long
    10.     dwPlatformID As Long
    11. End Type
    12.  
    13. Public Function UsesIE5(ByRef sVersionString As String) As Boolean
    14.     Dim dvi As DllVersionInfo
    15.    
    16.     dvi.cbSize = Len(dvi)
    17.     Call DllGetVersion(dvi)
    18.     sVersionString = dvi.dwMajorVersion & "." & dvi.dwMinorVersion & "." & _
    19.      dvi.dwBuildNumber
    20.     UsesIE5 = (dvi.dwMajorVersion >= 5)
    21. End Function
    Now simply use this:
    VB Code:
    1. Dim sIEVer As String
    2. If UsesIE5(sIEVer) = False Then
    3.     MsgBox "You are currently running version " & sIEVer & " of Internet Explorer" & vbCrLf & _
    4.      "This application requires version 5.0 or later.", vbInformation
    5. End If
    Last edited by Joacim Andersson; Aug 8th, 2006 at 11:36 PM.

  11. #11
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: download file from net - w/o events

    Quote Originally Posted by Joacim Andersson
    Well, putting IE5 or later as a requirement for your application should not be a problem. Besides you can always use code like the following to test if it's installed or not before calling URLDownloadToFile
    I agree. The whole point of my post was to make i00 aware of the restriction.
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  12. #12
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: download file from net - w/o events

    @Rory. What's the difference between your post and the code I posted earlier?

  13. #13
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: download file from net - w/o events

    Its a function so it tells the user when it is downloaded . ..

  14. #14
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: download file from net - w/o events

    Quote Originally Posted by rory
    Its a function so it tells the user when it is downloaded . ..
    No it doesn't. It tells the user if it was able to download it perhaps. Neither the function nor the sub returns until the file is downloaded (or the download failed for some reason).

  15. #15
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: download file from net - w/o events

    Quote Originally Posted by Joacim Andersson
    No it doesn't. It tells the user if it was able to download it perhaps. Neither the function nor the sub returns until the file is downloaded (or the download failed for some reason).
    thats what i said .. it tells them when it is downloaded. (not when it is downloading)

    Edit. .. whoops excuse my Bahamian, that should be "once it has completed the download" .. (1am to my defence)
    Last edited by rory; Aug 9th, 2006 at 12:21 AM.

  16. #16
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: download file from net - w/o events

    this one gives you a progress as it is downloaded .. check it out ..
    just threw it together tonight

    ps. not for large file downloads. ..
    Last edited by rory; Aug 9th, 2006 at 04:52 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width