Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Downloading A File From The Internet 1

Status
Not open for further replies.

yacyac

Programmer
Jul 29, 2002
78
US
I want the user to download a known file from a known location on the internet by just clicking a button, without them having to view the web page. In other words going to the website would be transparent to them, all they know is that a file is being downloaded. These files are typically text files or exe files used to upgrade their program.

Am I better of using FTP or HTTP to do this. I have absolutely no clue on how to do this or where to look to figure it out. Can anyone help with some sample code.

Thanks
 
trollacious, thanks for the information. I don't need any additional code before linking? I would use this as a typical filename, which I could then filecopy?
 
How do you have the code set up to access an HTTP server? Is this a browser type window? Are you using winsock?
 
Yacyac,

(...Here's one I made earlier...)

Depending on your version of VB, you could use the Internet Transfer Control.

The following code is by no means complete, but it should get you started...

Add a textbox, commandbutton and an internet transfer control to a form...

Text1 will be the full url to download.

If you look at the code, the most complex bits are sorting out a filename! The real "bones" of the job happen on the lines:

bData() = Inet1.OpenURL(strURL, icByteArray)
(This opens theURL into a bytearray)
Put #intFile, , bData()
(Saves the resulting array to disk)

Code:
Private Sub Command1_Click()
    Dim strURL As String, strFileName As String
    Dim bData() As Byte      ' Data variable
    Dim intFile As Integer   ' FreeFile variable
    Dim lngLen As Long
    strURL = Text1.Text
    intFile = FreeFile()      ' Set intFile to an unused
    lngLen = Len(strURL)
    
    Do
        lngLen = lngLen - 1
        If Mid(strURL, lngLen, 1) = "\" Then Exit Do
        If Mid(strURL, lngLen, 1) = "/" Then Exit Do
    Loop
    
    strFileName = Right(strURL, Len(strURL) - lngLen)
    ' Get the URL as a byte array
    bData() = Inet1.OpenURL(strURL, icByteArray)
    Open "c:\" & strFileName For Binary Access Write As #intFile
    'Save the byte array to disk
    Put #intFile, , bData()
    Close #intFile
    MsgBox "Grabbed " & strFileName & " to the desktop!"
    
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top