download a file from website not working
Hi,
I used the below code in my project but the result was not what I expected. Can anyone help explain why the file downloaded is the same file every time despite I uploaded an updated version file with the same file name to the website?
Here is the scenario, I put a text file named "test.txt" with the content of "1234" on an Internet website. When the code below is executed the very first time, the file was downloaded, and everything is working as expected. However, if I put an updated text file with the same name but changed the content to "abcd" on the website. Now when I execute the code again to download test.txt, the file downloaded successfully but the content is still the same, "1234", despite I deleted the file in c:\test.txt. So far I can fix this problem only if I went into the browser setting and cleared the history, deleted the cookies, and delete Internet temporary files, then the download will get the updated file posted on the web with the new content of "abcd". It seemed as if it cached the file name in IE for some reasons.
Any helps would be greatly appreciated.
VB Code:
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
Public Function DownloadFile(URL As String, LocalFilename As String) As Boolean
Dim lngRetVal As Long
lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
If lngRetVal = 0 Then DownloadFile = True
End Function
Sub main()
DownloadFile "http://mywebsite.home.comcast.net/test.txt", "c:\test.txt"
end sub
Re: download a file from website not working
try this
Code:
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
****************************************************
Sub main()
Dim DFile, FullLink As String
DFile = "C:\test.txt"
FullLink = "http://mywebsite.home.comcast.net/test.txt"
DownloadData
End Sub
****************************************************
Sub DownloadData()
If URLDownloadToFile(0, FullLink, DFold & DFile, 0, 0) = 0 Then
Workbooks.Open Filename:=DFile
End If
End Sub
ska
Re: download a file from website not working