Tony789
asked on
Downloading files from URL in VB 6
I am using the below code to download a file from a URL. I am receiving the "Write to file failed message" even though I have given IUSR write permission to the folder.
Private Sub Command1_Click()
strFileURL = "http://www.url.com/download.exe"
strHDLocation = "C:\DWTest"
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
objXMLHTTP.Open "GET", strFileURL, False
objXMLHTTP.send
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1
objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
Set objFSO = Nothing
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
End If
Set objXMLHTTP = Nothing
End Sub
To pass username/password using the XMLHTTP object, make the changes below. The Open method supports Windows authentication.
Private Sub Command1_Click()
strFileURL = "http://www.url.com/download.exe"
strHDLocation = "C:\DWTest"
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
objXMLHTTP.Open "GET", strFileURL, False, "username", "password"
objXMLHTTP.send
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1
objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
Set objFSO = Nothing
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
End If
Set objXMLHTTP = Nothing
End Sub
ASKER
Is the password being sent in clear text across the internet? Is this secure?
I am still getting the "Write to file failed message" when downloading. Can you please let me know what permissions I am missing in the destination folder?
I am still getting the "Write to file failed message" when downloading. Can you please let me know what permissions I am missing in the destination folder?
I think the error is to do with the ADODB.Stream object. You can use the FileSystemObject to write the response to disk, as below.
The username and password as sent by the MSXML2.XMLHTTP object will be sent using whatever authentication method is supported by the server at the other end - if possible encryption will be used, but if not it will be clear text.
The username and password as sent by the MSXML2.XMLHTTP object will be sent using whatever authentication method is supported by the server at the other end - if possible encryption will be used, but if not it will be clear text.
Private Sub Command1_Click()
strFileURL = "http://www.url.com/download.exe"
strHDLocation = "C:\DWTest"
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
objXMLHTTP.Open "GET", strFileURL, False, "username", "password"
objXMLHTTP.send
Set objFSO = CreateObject("Scripting.FileSystemObject")
set objF=objFSO.CreateTextFile strHDLocation, true ' Overwrite if exists
objF.Write objXMLHTTP.ResponseText
objF.Close
set objF=Nothing
Set objFSO = Nothing
End If
Set objXMLHTTP = Nothing
End Sub
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
I was presuming that c:\DWTest was a file to which the results were to be saved - as there was a specific DeleteFile called on this. Good point though, that may well be the issue!
Thanks for the grade.
@Purple....the objADOStream.SaveToFile method needs to have a file name passed to it. If there's a folder that already exists, that method will fail. Seeing as C:\DWTest might be a folder, it's best to have the filename from the required file used, instead of a constant one.
Regards,
Rob.
@Purple....the objADOStream.SaveToFile method needs to have a file name passed to it. If there's a folder that already exists, that method will fail. Seeing as C:\DWTest might be a folder, it's best to have the filename from the required file used, instead of a constant one.
Regards,
Rob.
ASKER