Results 1 to 7 of 7

Thread: Weird download problem

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2010
    Location
    Wodonga, Aus
    Posts
    33

    Weird download problem

    Hi, I am making a small application to automatically download an image that updates every hour. It works, except the application seems to download duplicates of the same image, even though when I visit the URL myself, it shows the newest image.

    I thought something might have been in the cache, and it was just using that instead. I have inserted some cache clearing code, however was just wondering (While I wait for the hour ) if anyone had any other idea's as to why it would do this?

    My code (without cache clearing stuff)

    Code:
    Dim Hour As Integer
    
    Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
        "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, _
        ByVal szFileName As String, ByVal dwReserved As Long, _
        ByVal lpfnCB As Long) As Long
        
    Dim errcode As Long
    Dim url As String
    Dim localFileName As String
    
    
    
    Private Sub StartBtn_Click()
    Timer1.Enabled = True
    End Sub
    
    Private Sub Stop_Click()
    Timer1.Enabled = False
    Hour = 0
    End Sub
    
    Private Sub Timer1_Timer()
    Hour = Hour + 1
    Label2.Caption = "Seconds until next image: " & 3600 - Hour
    If Hour = 3600 Then
        Hour = 0
        Dim CDT As String
        Dim CDT1 As String
        Dim CDT2 As String
        CDT = Now
        CDT1 = Replace(CDT, "/", "-")
        CDT2 = Replace(CDT1, ":", ".")
        url = "http://www.goes.noaa.gov/sohemi/sohemiloops/ircolgms/8.jpg"
        localFileName = "C:\Loops\" & CDT2 & ".jpg"
        errcode = URLDownloadToFile(0, url, localFileName, 0, 0)
        If errcode = 0 Then
        Label3.Caption = "Download Successful"
        Label1.Caption = "Last download: " & Now
        Else
        Label3.Caption = "Error Downloading"
        Label1.Caption = "Failed download: " & Now
    End If
    End If
    End Sub
    Regards,
    Dan

  2. #2
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Weird download problem

    Use the BINDF_GETNEWESTVERSION flag.

    vb Code:
    1. Private Const BINDF_GETNEWESTVERSION As Long = &H10
    2.  
    3. errcode = URLDownloadToFile(0, url, localFileName, BINDF_GETNEWESTVERSION, 0)

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2010
    Location
    Wodonga, Aus
    Posts
    33

    Re: Weird download problem

    Well, that's odd, it still downloaded the same image twice

    Code:
    Dim Hour As Integer
    
    Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
        "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, _
        ByVal szFileName As String, ByVal dwReserved As Long, _
        ByVal lpfnCB As Long) As Long
        
    Dim errcode As Long
    Dim url As String
    Dim localFileName As String
    Private Const BINDF_GETNEWESTVERSION As Long = &H10
    
    
    
    Private Sub StartBtn_Click()
        Dim CDT As String
        Dim CDT1 As String
        Dim CDT2 As String
        CDT = Now
        CDT1 = Replace(CDT, "/", "-")
        CDT2 = Replace(CDT1, ":", ".")
        url = "http://www.goes.noaa.gov/sohemi/sohemiloops/ircolgms/8.jpg"
        localFileName = "C:\Loops\" & CDT2 & ".jpg"
        errcode = URLDownloadToFile(0, url, localFileName, BINDF_GETNEWESTVERSION, 0)
        If errcode = 0 Then
        Label3.Caption = "Download Successful"
        Label1.Caption = "Last download: " & Now
        Else
        Label3.Caption = "Error Downloading"
        Label1.Caption = "Failed download: " & Now
    End If
    Timer1.Enabled = True
    End Sub
    
    Private Sub Stop_Click()
    Timer1.Enabled = False
    Hour = 0
    End Sub
    
    Private Sub Timer1_Timer()
    Hour = Hour + 1
    Label2.Caption = "Seconds until next image: " & 3600 - Hour
    If Hour = 3600 Then
        Hour = 0
        Dim CDT As String
        Dim CDT1 As String
        Dim CDT2 As String
        CDT = Now
        CDT1 = Replace(CDT, "/", "-")
        CDT2 = Replace(CDT1, ":", ".")
        url = "http://www.goes.noaa.gov/sohemi/sohemiloops/ircolgms/8.jpg"
        localFileName = "C:\Loops\" & CDT2 & ".jpg"
        errcode = URLDownloadToFile(0, url, localFileName, BINDF_GETNEWESTVERSION, 0)
        If errcode = 0 Then
        Label3.Caption = "Download Successful"
        Label1.Caption = "Last download: " & Now
        Else
        Label3.Caption = "Error Downloading"
        Label1.Caption = "Failed download: " & Now
    End If
    End If
    End Sub
    Regards,
    Dan

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Weird download problem

    I don't use that API function, but any official documentation I can find on MSDN indicates BINDF_GETNEWESTVERSION is not used with URLDownloadToFile but with URLDownloadToCacheFile instead.

    The "command2" example at this link, clears the cache first then downloads the target. Note that example is also using BINDF_GETNEWESTVERSION & maybe improperly.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Weird download problem

    Hmm, I tested BINDF_GETNEWESTVERSION with URLDownloadToFile a while ago and it worked fine.

    It is also used on vbnet.mvps.org.

    http://vbnet.mvps.org/index.html?cod...loadtofile.htm

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2010
    Location
    Wodonga, Aus
    Posts
    33

    Re: Weird download problem

    Hmm, seems to be working now. Looks like it only saves duplicates of the first 2 images.

    Cheers,
    Dan

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Weird download problem

    i had a similar problem downloading updated vb6 exe, using firefox
    so i made a small app using urldownloadtofile, to clear cache and always download the new version of exe
    worked fine for me
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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