Link to home
Start Free TrialLog in
Avatar of sydneyguy
sydneyguyFlag for Australia

asked on

saving a tesxt file given a http link from a server address to the local drive

Vb net experts an easy one for you
I am using outlook to download and save a file from the server to my hard disk.
I have a email that comes in with details and a http link which needs to be saved onto my hard disk.
I have succeded in extraction the address eg
http://www.abc.com/media/uploads/2007-07-25-07-19-Unknown-Fjob.txt
   
I have been playing with and looking at using the below code

 If Left(Res, 7) = "http://" Then
            furl = Res   '' this should turn out to be "http://www.somesite.com/some/paths/file.zip"
            filn = "C:\Media\" & Mid(furl, InStrRev(furl, "/") + 1) ''
            Set XH = CreateObject("Microsoft.XMLHTTP") '' use "Microsoft XML 6.0" to download
            XH.Open "GET", furl, 0
            X = XH.Send()
            Set XG = CreateObject("ADODB.Stream") '' use "Microsoft ActiveX Data Objects 2.8 Library" to save to decode and save to disk
            XG.Mode = 3: XG.Type = 1
            X = XG.Open()
            XG.Write (XH.responseBody)
            XG.SaveToFile filn, 2
           ' Exit For    '' only download first link found
       End If

which all works up to the point of XG.SaveToFile filn, 2 which then comes up with "variable not defined"

also tried

With CommonDialog1
    .Action = 2
    .DialogTitle = "Save File"
    .InitDir = ""
    .Filter = "Saved Files (*.htm)|*.htm"
    .FilterIndex = 1
    .Flags = cdlOFNNoValidate + cdlOFNHideReadOnly + lOFNExplorer
    .filename = "testname.txt" 'ParsePath(sFile, 6)
    .ShowSave
    sFile = .filename
  End With

run time error  424  "object required"
not sure which is best and what needs to be changed

now I am not sure what I am doing here and have not been able to find any understandable working code
all I need to do is to down load the file to my hard disk frm my server any code that works will be appreciated
thanks

Avatar of PaulHews
PaulHews
Flag of Canada image

Dim xmlHTTP As MSXML2.xmlHTTP
Dim strURL As String

Set xmlHTTP = CreateObject("MSXML2.xmlHTTP")
strURL = "http://www.abc.com/media/uploads/2007-07-25-07-19-Unknown-Fjob.txt"
xmlHTTP.Open "GET", strURL, False
xmlHTTP.send
Call sPutBytesToFile(xmlHTTP.responseBody, "2007-07-25-07-19-Unknown-Fjob.txt")




Public Sub sPutBytesToFile(byt() As Byte, strPath As String)
    Dim i As Integer
    Dim hFile  As Integer
    If Dir(strPath) <> "" Then
        Kill strPath
    End If

    hFile = FreeFile
    Open strPath For Binary As #hFile
    Put #hFile, , byt
    Close #hFile

End Sub
Note that you're going to want to set the "save file" path better than I did there... :)
ASKER CERTIFIED SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial