Uploading files to the internet using vb

Posted on 2004-09-14
Last Modified: 2010-05-02
I want to upload a report in the form of an excel spreadsheet to a website (really intranet). I am new to this and most of the examples I find are for downloading from the internet. Does anyone have code I can look at to get me going?
Question by:bmugabe
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 32

Expert Comment

ID: 12056948
See this example:

FTP Files Using the Internet Transfer Control

Assisted Solution

mladenovicz earned 50 total points
ID: 12056967
here is ftp sample

Const INTERNET_DEFAULT_FTP_PORT = 21               ' default for FTP servers
Const INTERNET_FLAG_PASSIVE = &H8000000            ' used for FTP connections
Const INTERNET_OPEN_TYPE_PRECONFIG = 0                    ' use registry configuration
Const INTERNET_OPEN_TYPE_DIRECT = 1                        ' direct to net
Const INTERNET_OPEN_TYPE_PROXY = 3                         ' via named proxy
Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4   ' prevent using java/script/INS
Const MAX_PATH = 260
Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * MAX_PATH
    cAlternate As String * 14
End Type
Private Declare Function InternetCloseHandle Lib "wininet" (ByRef hInet As Long) As Long
Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUserName As String, ByVal sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As String, lpdwCurrentDirectory As Long) As Long
Private Declare Function FtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpRemoveDirectory Lib "wininet.dll" Alias "FtpRemoveDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
Private Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" (ByVal hFtpSession As Long, ByVal lpszExisting As String, ByVal lpszNew As String) As Boolean
Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hConnect As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Long, ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, ByRef dwContext As Long) As Boolean
Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hConnect As Long, ByVal lpszLocalFile As String, ByVal lpszNewRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias "InternetGetLastResponseInfoA" (lpdwError As Long, ByVal lpszBuffer As String, lpdwBufferLength As Long) As Boolean
Private Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContent As Long) As Long
Private Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long
Const PassiveConnection As Boolean = True
Private Sub Form_Load()
    'KPD-Team 2000
    Dim hConnection As Long, hOpen As Long, sOrgPath  As String
    'open an internet connection
    hOpen = InternetOpen("API-Guide sample program", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
    'connect to the FTP server
    hConnection = InternetConnect(hOpen, "your ftp server", INTERNET_DEFAULT_FTP_PORT, "your login", "your password", INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
    'create a buffer to store the original directory
    sOrgPath = String(MAX_PATH, 0)
    'get the directory
    FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
    'create a new directory 'testing'
    FtpCreateDirectory hConnection, "testing"
    'set the current directory to 'root/testing'
    FtpSetCurrentDirectory hConnection, "testing"
    'upload the file 'test.htm'
    FtpPutFile hConnection, "C:\test.htm", "test.htm", FTP_TRANSFER_TYPE_UNKNOWN, 0
    'rename 'test.htm' to 'apiguide.htm'
    FtpRenameFile hConnection, "test.htm", "apiguide.htm"
    'enumerate the file list from the current directory ('root/testing')
    EnumFiles hConnection
    'retrieve the file from the FTP server
    FtpGetFile hConnection, "apiguide.htm", "c:\apiguide.htm", False, 0, FTP_TRANSFER_TYPE_UNKNOWN, 0
    'delete the file from the FTP server
    FtpDeleteFile hConnection, "apiguide.htm"
    'set the current directory back to the root
    FtpSetCurrentDirectory hConnection, sOrgPath
    'remove the direcrtory 'testing'
    FtpRemoveDirectory hConnection, "testing"
    'close the FTP connection
    InternetCloseHandle hConnection
    'close the internet connection
    InternetCloseHandle hOpen
End Sub
Public Sub EnumFiles(hConnection As Long)
    Dim pData As WIN32_FIND_DATA, hFind As Long, lRet As Long
    'set the graphics mode to persistent
    Me.AutoRedraw = True
    'create a buffer
    pData.cFileName = String(MAX_PATH, 0)
    'find the first file
    hFind = FtpFindFirstFile(hConnection, "*.*", pData, 0, 0)
    'if there's no file, then exit sub
    If hFind = 0 Then Exit Sub
    'show the filename
    Me.Print Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
        'create a buffer
        pData.cFileName = String(MAX_PATH, 0)
        'find the next file
        lRet = InternetFindNextFile(hFind, pData)
        'if there's no next file, exit do
        If lRet = 0 Then Exit Do
        'show the filename
        Me.Print Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
    'close the search handle
    InternetCloseHandle hFind
End Sub
Sub ShowError()
    Dim lErr As Long, sErr As String, lenBuf As Long
    'get the required buffer size
    InternetGetLastResponseInfo lErr, sErr, lenBuf
    'create a buffer
    sErr = String(lenBuf, 0)
    'retrieve the last respons info
    InternetGetLastResponseInfo lErr, sErr, lenBuf
    'show the last response info
    MsgBox "Error " + CStr(lErr) + ": " + sErr, vbOKOnly + vbCritical
End Sub

Author Comment

ID: 12058098
Do you have sample for http file transfer?
LVL 19

Accepted Solution

RanjeetRain earned 150 total points
ID: 12058844
Use the Internet Transfer Control. It is capable of doing HTTP transfers as well. The verb to execute would be PUT. YOu must have write permission in the folder you attempt uplading the file.

(1) Create a form and put a Internet Transfer Control control on it
(2) Set the Protocol property of the control to 4 (icHTTP)
(3) Specify Remote host (the IP of your webserver) and Remote port (generally 80)
(4) Specify URL as PUT and the location where you have to upload (e.g. /uploads/) and name of the file to be uplaoded, such as c:\source.xls

Assisted Solution

sgayatri earned 50 total points
ID: 12062684
This module is working code

It uses the standard FTP.exe of windows

Generates three text files, the first two  are temporary intermediate files
ftpmain.bat - which contains the actual ftp command and takes    ftptest.bat as an input
ftptest.bat - which is a command file that gets created in the code
ftp.log - the log file written during ftp

Write your own interface (i.e., vb form) call the following module
assign values where necessary

(variables fname -- file to be FTPied
 FTPSERVER - IPadrr or alias of the server )

this code gets executed in background

Private Sub ftp_send_receive_dos()
''' Give the ip address or server alias at the place
''' where FTPSERVER is mentioned in this code

comd = "put"   ''' change this to put or get as per requirement

Set fso = CreateObject("scripting.filesystemobject")
Set f = CreateObject("scripting.filesystemobject")

outfile = fname ''' fname is the file to be FTPied

If comd = "put" And f.FileExists(outfile) = False Then
    MsgBox " File is not existing "
    Exit Sub
End If

''''delete old files

If f.FileExists("c:\windows\temp\ftpmain.bat") = True Then
    f.DeleteFile ("c:\windows\temp\ftpmain.bat")
End If
If f.FileExists("c:\windows\temp\ftptest") = True Then
    f.DeleteFile ("c:\windows\temp\ftptest")
End If

''' create ftpmain.bat

Set mainfile = fso.CreateTextFile("c:\windows\temp\ftpmain.bat")
mainfile.WriteLine ("ftp -s:c:\windows\temp\ftptest.bat FTPSERVER > c:\windows\desktop\ftp.log")


''' create ftptest.bat

Set batfile = fso.CreateTextFile("c:\windows\temp\ftptest.bat")
batfile.WriteLine ("USERID")
batfile.WriteBlankLines (1)
batfile.WriteLine ("PASSWORD")
batfile.WriteBlankLines (1)
batfile.WriteLine ("bin")
batfile.WriteBlankLines (1)
batfile.WriteBlankLines (1)
batfile.WriteLine ("cd ftpdocs")
batfile.WriteBlankLines (1)
batfile.WriteLine (comd & " " & outfile)
batfile.WriteBlankLines (1)
batfile.WriteLine ("bye")

''' run the ftpmain.bat

Shell "  /c  c:\windows\temp\ftpmain.bat", vbHide

length = -1
While f.FileExists("c:\windows\desktop\ftp.log") = False
   Sleep 1000
Label2.Caption = "FTP Started..."
Sleep 100

ftpprg = "FTP in Progress "
polinterval = 10000   ' 10 sec
mins = 0
cnt = 0
While FileLen("c:\windows\desktop\ftp.log") <> length

    If FileLen("c:\windows\desktop\ftp.log") > 0 Then
       length = FileLen("c:\windows\desktop\ftp.log")
    End If
    Sleep polinterval

    cnt = cnt + 1
    'If Cnt = 6 Then
    If cnt = (60000 / polinterval) Then
        mins = mins + 1
        cnt = 0
        ftpprg = "FTP in Progress " & mins 'ftpprg & mins
        ftpprg = ftpprg & "."
        Label2.Caption = ftpprg
    End If
   'Sleep 3000
'pbar.Value = pbar.Max
MsgBox "file length is " & length

f.DeleteFile ("c:\windows\temp\ftpmain.bat")
f.DeleteFile ("c:\windows\temp\ftptest.bat")

end sub

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Spell Check in VB6 13 137
How to hault or freeze parent form when a 2d form is open in vb6 3 43
Added a column screws up code 5 69
transition to visual .net from vb6 5 63
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question