Saving a www file on a remote server locally on my PC using VB 
Author Message
 Saving a www file on a remote server locally on my PC using VB

Hi,
    anyone got any ideas on how to do it?
Cheers
Paul Mitchell


Fri, 21 Oct 2005 03:32:26 GMT  
 Saving a www file on a remote server locally on my PC using VB

Quote:

> Hi,
>     anyone got any ideas on how to do it?
> Cheers
> Paul Mitchell

Easiest way is to use the Inet control. Assuming you know how to add that to your form use this.
It'll only work if the file you are downloading is not a binary file. If you want to download a
binary file there are other methods used to save etc.

Private Sub GetFile(RemotePath As String, LocalPath As String)
Dim a As String
'Cancel any operations
Inet1.Cancel
'Set protocol to HTTP
Inet1.Protocol = icHTTP
'Set the URL Property
Inet1.URL = RemotePath
'Retrieve the HTML data into a byte array.
a = Inet1.OpenURL(, icString)
'Create a tempory file for the page to reside
Open LocalPath For Output As #1
    Print #1, a
Close #1
End Sub

Private Sub Command1_Click()
GetFile "http://www.ask.com/index.asp", "C:\Temp\ask.asp"
MsgBox "Done"
End Sub

hope this helps

Alex



Fri, 21 Oct 2005 07:13:11 GMT  
 Saving a www file on a remote server locally on my PC using VB
Thank you Alex, that works fine.
Cheers
Paul Mitchell



Quote:
> > Hi,
> >     anyone got any ideas on how to do it?
> > Cheers
> > Paul Mitchell

> Easiest way is to use the Inet control. Assuming you know how to add that

to your form use this.
Quote:
> It'll only work if the file you are downloading is not a binary file. If

you want to download a
Quote:
> binary file there are other methods used to save etc.

> Private Sub GetFile(RemotePath As String, LocalPath As String)
> Dim a As String
> 'Cancel any operations
> Inet1.Cancel
> 'Set protocol to HTTP
> Inet1.Protocol = icHTTP
> 'Set the URL Property
> Inet1.URL = RemotePath
> 'Retrieve the HTML data into a byte array.
> a = Inet1.OpenURL(, icString)
> 'Create a tempory file for the page to reside
> Open LocalPath For Output As #1
>     Print #1, a
> Close #1
> End Sub

> Private Sub Command1_Click()
> GetFile "http://www.ask.com/index.asp", "C:\Temp\ask.asp"
> MsgBox "Done"
> End Sub

> hope this helps

> Alex



Fri, 21 Oct 2005 16:58:24 GMT  
 Saving a www file on a remote server locally on my PC using VB
Sorry! It actually gets only the first 4096 characters of the file, is this
the max length of a string?

Cheers
Paul Mitchell


Quote:
> Thank you Alex, that works fine.
> Cheers
> Paul Mitchell





> > > Hi,
> > >     anyone got any ideas on how to do it?
> > > Cheers
> > > Paul Mitchell

> > Easiest way is to use the Inet control. Assuming you know how to add
that
> to your form use this.
> > It'll only work if the file you are downloading is not a binary file. If
> you want to download a
> > binary file there are other methods used to save etc.

> > Private Sub GetFile(RemotePath As String, LocalPath As String)
> > Dim a As String
> > 'Cancel any operations
> > Inet1.Cancel
> > 'Set protocol to HTTP
> > Inet1.Protocol = icHTTP
> > 'Set the URL Property
> > Inet1.URL = RemotePath
> > 'Retrieve the HTML data into a byte array.
> > a = Inet1.OpenURL(, icString)
> > 'Create a tempory file for the page to reside
> > Open LocalPath For Output As #1
> >     Print #1, a
> > Close #1
> > End Sub

> > Private Sub Command1_Click()
> > GetFile "http://www.ask.com/index.asp", "C:\Temp\ask.asp"
> > MsgBox "Done"
> > End Sub

> > hope this helps

> > Alex



Fri, 21 Oct 2005 18:17:56 GMT  
 Saving a www file on a remote server locally on my PC using VB
If its any use the page I was trying to save locally was:

http://www.metoffice.gov.uk/datafiles/offshore.html

Cheers
Paul


Quote:
> Sorry! It actually gets only the first 4096 characters of the file, is
this
> the max length of a string?

> Cheers
> Paul Mitchell



> > Thank you Alex, that works fine.
> > Cheers
> > Paul Mitchell





> > > > Hi,
> > > >     anyone got any ideas on how to do it?
> > > > Cheers
> > > > Paul Mitchell

> > > Easiest way is to use the Inet control. Assuming you know how to add
> that
> > to your form use this.
> > > It'll only work if the file you are downloading is not a binary file.
If
> > you want to download a
> > > binary file there are other methods used to save etc.

> > > Private Sub GetFile(RemotePath As String, LocalPath As String)
> > > Dim a As String
> > > 'Cancel any operations
> > > Inet1.Cancel
> > > 'Set protocol to HTTP
> > > Inet1.Protocol = icHTTP
> > > 'Set the URL Property
> > > Inet1.URL = RemotePath
> > > 'Retrieve the HTML data into a byte array.
> > > a = Inet1.OpenURL(, icString)
> > > 'Create a tempory file for the page to reside
> > > Open LocalPath For Output As #1
> > >     Print #1, a
> > > Close #1
> > > End Sub

> > > Private Sub Command1_Click()
> > > GetFile "http://www.ask.com/index.asp", "C:\Temp\ask.asp"
> > > MsgBox "Done"
> > > End Sub

> > > hope this helps

> > > Alex



Fri, 21 Oct 2005 18:30:49 GMT  
 Saving a www file on a remote server locally on my PC using VB
So it does. In that case I'm not sure how it's supposed to be done but I found this code from
vb2themax which uses the inet dll and so you don't need to add the control to your form:

Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Const INTERNET_FLAG_EXISTING_CONNECT = &H20000000

Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _
    (ByVal lpszAgent As String, ByVal dwAccessType As Long, _
    ByVal lpszProxyName As String, ByVal lpszProxyBypass As String, _
    ByVal dwFlags As Long) As Long
Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias _
    "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal lpszUrl As String, _
    ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, _
    ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As _
    Long) As Integer
Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As _
    Long, ByVal lpBuffer As String, ByVal dwNumberOfBytesToRead As Long, _
    lNumberOfBytesRead As Long) As Integer

' Download a file from the Internet and save it to a local file
'
' it works with HTTP and FTP, but you must explicitly include
' the protocol name in the URL, as in
'    CopyURLToFile "http://www.vb2themax.com/default.asp", "C:\vb2themax.htm"

Sub CopyURLToFile(ByVal URL As String, ByVal FileName As String)
    Dim hInternetSession As Long
    Dim hUrl As Long
    Dim FileNum As Integer
    Dim ok As Boolean
    Dim NumberOfBytesRead As Long
    Dim Buffer As String
    Dim fileIsOpen As Boolean

    On Error GoTo ErrorHandler

    ' check obvious syntax errors
    If Len(URL) = 0 Or Len(FileName) = 0 Then Err.Raise 5

    ' open an Internet session, and retrieve its handle
    hInternetSession = InternetOpen(App.EXEName, INTERNET_OPEN_TYPE_PRECONFIG, _
        vbNullString, vbNullString, 0)
    If hInternetSession = 0 Then
        Err.Raise vbObjectError + 1000, , _
            "An error occurred calling InternetOpen function"
    End If

    ' open the file and retrieve its handle
    hUrl = InternetOpenUrl(hInternetSession, URL, vbNullString, 0, _
        INTERNET_FLAG_EXISTING_CONNECT, 0)
    If hUrl = 0 Then
        Err.Raise vbObjectError + 1000, , _
            "An error occurred calling InternetOpenUrl function"
    End If

    ' ensure that there is no local file
    On Error Resume Next
    Kill FileName

    On Error GoTo ErrorHandler

    ' open the local file
    FileNum = FreeFile
    Open FileName For Binary As FileNum
    fileIsOpen = True

    ' prepare the receiving buffer
    Buffer = Space(1024)

    Do
        ' read a chunk of the file - returns True if no error
        ok = InternetReadFile(hUrl, Buffer, Len(Buffer), NumberOfBytesRead)

        ' exit if error or no more data
        If NumberOfBytesRead = 0 Or Not ok Then Exit Do

        ' save the data to the local file
        Put #FileNum, , Left$(Buffer, NumberOfBytesRead)
        DoEvents
    Loop

    ' flow into the error handler

ErrorHandler:
    ' close the local file, if necessary
    If fileIsOpen Then Close #FileNum
    ' close internet handles, if necessary
    If hUrl Then InternetCloseHandle hUrl
    If hInternetSession Then InternetCloseHandle hInternetSession

    ' report the error to the client, if there is one
    If Err Then Err.Raise Err.Number, , Err.Description
End Sub

Alex

Quote:

> Sorry! It actually gets only the first 4096 characters of the file, is this
> the max length of a string?

> Cheers
> Paul Mitchell



> > Thank you Alex, that works fine.
> > Cheers
> > Paul Mitchell





> > > > Hi,
> > > >     anyone got any ideas on how to do it?
> > > > Cheers
> > > > Paul Mitchell

> > > Easiest way is to use the Inet control. Assuming you know how to add
> that
> > to your form use this.
> > > It'll only work if the file you are downloading is not a binary file. If
> > you want to download a
> > > binary file there are other methods used to save etc.

> > > Private Sub GetFile(RemotePath As String, LocalPath As String)
> > > Dim a As String
> > > 'Cancel any operations
> > > Inet1.Cancel
> > > 'Set protocol to HTTP
> > > Inet1.Protocol = icHTTP
> > > 'Set the URL Property
> > > Inet1.URL = RemotePath
> > > 'Retrieve the HTML data into a byte array.
> > > a = Inet1.OpenURL(, icString)
> > > 'Create a tempory file for the page to reside
> > > Open LocalPath For Output As #1
> > >     Print #1, a
> > > Close #1
> > > End Sub

> > > Private Sub Command1_Click()
> > > GetFile "http://www.ask.com/index.asp", "C:\Temp\ask.asp"
> > > MsgBox "Done"
> > > End Sub

> > > hope this helps

> > > Alex



Fri, 21 Oct 2005 22:35:33 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Query server and save file locally

2. Using VB script to create a file on a remote server

3. Import xls file into SQL Server using VBscript and exe function on remote server

4. Saving file to remote server

5. Saving files to remote servers

6. howto: print server file locally

7. howto: print server file locally

8. Problems installing a server-program on a remote PC

9. Transfer File from one PC to another PC on LAN using winsock

10. Running VB on a remote server using rcmd

11. Remote Server Time using VB

12. Remote VB client has trouble with STA server that uses custom MessageFilter

 

 
Powered by phpBB® Forum Software