|
-
Sep 2nd, 2010, 05:29 AM
#1
Thread Starter
Member
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
-
Sep 2nd, 2010, 07:50 AM
#2
Re: Weird download problem
Use the BINDF_GETNEWESTVERSION flag.
vb Code:
Private Const BINDF_GETNEWESTVERSION As Long = &H10
errcode = URLDownloadToFile(0, url, localFileName, BINDF_GETNEWESTVERSION, 0)
-
Sep 2nd, 2010, 09:01 AM
#3
Thread Starter
Member
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
-
Sep 2nd, 2010, 09:35 AM
#4
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.
-
Sep 2nd, 2010, 11:50 AM
#5
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
-
Sep 3rd, 2010, 01:02 AM
#6
Thread Starter
Member
Re: Weird download problem
Hmm, seems to be working now. Looks like it only saves duplicates of the first 2 images.
Cheers,
Dan
-
Sep 3rd, 2010, 03:43 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|