[VB.NET 2003] FTP upload suceeded. but no file transferred?

T

thomasc

Hello,
I wrote a simple FTP code which monitors a user-specified folder and,
if a new file is generated in the folder, uploads it to an FTP
server.

The program seems to be working on Visual Studio .NET 2003 IDE, and
the FTP server program says it has sucessfully received the file. But
I can't find the transferred file in the destination folder in FTP
server.

I have no idea why this happens.
I activated an FTP server on the same PC on which I run this program
on Visual Studio .NET 2003 IDE. Can it be why and I need to run the
program other than the PC where the FTP server is running?

Please take a look at my codes below and let me know what I'm doing
wrong.
Thank you mush in advance.


<DESCRIPTION>
On the form, I have put
a Button1 which is linked to a FolderBrowserDialog to select the
folder to monitor,
a TextBox1 to display the selected folder path by Button1, and
a button named "Start" used to start monitoring.

Below are my codes for this program:
- ftp.vb(the FTP function)
- Form1.vb(the main function),
- Class1.vb(public variables),



'''''''''''''''''''''''''''''' ftp.vb
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Module ftp

' Declare the API functions
' The three that you will use are FtpGetFile (for downloading),
FtpPutFile (for uploading), and InternetConnect (to specify the server
and login credentials)
Private Declare Function InternetCloseHandle Lib
"wininet.dll" (ByVal HINet As Integer) As Integer
Private Declare Function InternetOpen Lib "wininet.dll" Alias
"InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Integer,
ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags
As Integer) As Integer
Private Declare Function InternetConnect Lib "wininet.dll" Alias
"InternetConnectA" (ByVal hInternetSession As Integer, ByVal
sServerName As String, ByVal nServerPort As Integer, ByVal sUsername
As String, ByVal sPassword As String, ByVal lService As Integer, ByVal
lFlags As Integer, ByVal lContext As Integer) As Integer
Private Declare Function FtpGetFile Lib "wininet.dll" Alias
"FtpGetFileA" (ByVal hFtpSession As Integer, ByVal lpszRemoteFile As
String, ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean,
ByVal dwFlagsAndAttributes As Integer, ByVal dwFlags As Integer, ByVal
dwContext As Integer) As Boolean
Private Declare Function FtpPutFile Lib "wininet.dll" Alias
"FtpPutFileA" (ByVal hFtpSession As Integer, ByVal lpszLocalFile As
String, ByVal lpszRemoteFile As String, ByVal dwFlags As Integer,
ByVal dwContext As Integer) As Boolean

Const FTP_TRANSFER_TYPE_UNKNOWN = &H0
Const FTP_TRANSFER_TYPE_ASCII = &H1
Const FTP_TRANSFER_TYPE_BINARY = &H2
Const INTERNET_DEFAULT_FTP_PORT = 21 ' default For FTP servers
Const INTERNET_SERVICE_FTP = 1
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
Const PassiveConnection As Boolean = True

Sub Upload()
Dim INet, INetConn As Integer
Dim RC As Boolean

INet = InternetOpen("FTP Connection",
INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
INetConn = InternetConnect(INet, "192.168.200.1", 21, "test",
"test", 1, 0, 0)
RC = FtpPutFile(INetConn, srcFilePath, destPath,
FTP_TRANSFER_TYPE_BINARY, 0)

If RC Then MsgBox("File uploaded!")
If Not RC Then
err =
System.Runtime.InteropServices.Marshal.GetLastWin32Error()
MsgBox("Error occured")
InternetCloseHandle(INetConn)
End If
InternetCloseHandle(INet)
End Sub

' example of how to use the FtpGetFile Function
Sub Download()
Dim INet, INetConn As Integer
Dim RC As Boolean

INet = InternetOpen("FTP Connection", 1, vbNullString,
vbNullString, 0)
INetConn = InternetConnect(INet, "192.168.200,1",
INTERNET_DEFAULT_FTP_PORT, "test", "test", 1, 0, 0)
RC = FtpGetFile(INetConn, "./myimage.png", "C:\myimage.png",
False, 1, 0, 0)
If RC Then MsgBox("picture Downloaded!")
If Not RC Then MsgBox("error, could not connect")
InternetCloseHandle(INetConn)
InternetCloseHandle(INet)

End Sub


End Module






'''''''''''''''''''''''''''''' Form1.vb
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Imports System.io

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Code generate by Windows Form designer"

Dim watcher1Set As Boolean
Dim sentDir As String

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
'set the folder to monitor
FolderBrowserDialog1.ShowNewFolderButton = True
Dim result As DialogResult = FolderBrowserDialog1.ShowDialog()

If (result = DialogResult.OK) Then
srcPath = FolderBrowserDialog1.SelectedPath
End If
TextBox1.Text = srcPath
watcher1Set = True
End Sub

'Event Handler1
Private Shared Sub OnCreated1(ByVal source As Object, ByVal e As
FileSystemEventArgs)
Dim NewFilePath, DateFromFileName, sentPath, destNewFilePath
As String
NewFilePath = e.FullPath
NewFileName = e.Name
DateFromFileName = NewFileName.Substring(0, 8)
srcFilePath = srcPath + "\" + NewFileName
destPath = "./" + NewFileName
Upload()

End Sub

Private Sub Start_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Start.Click

'FileSystemWatcher object
If watcher1Set = True Then
Dim watcher1 As New FileSystemWatcher
watcher1.Path = srcPath
'watcher1.Filter = "*." + filter1
'determine what to watch
watcher1.NotifyFilter = (NotifyFilters.LastAccess Or
NotifyFilters.LastWrite Or NotifyFilters.FileName)
'Add event handlers.
AddHandler watcher1.Created, AddressOf OnCreated1
'Begin watching.
watcher1.EnableRaisingEvents = True
End If

'check to see if 'sent' folder exists under srcPath, if not
make one
sentDir = srcPath & "\sent\"
If System.IO.Directory.Exists(sentDir) = False Then
System.IO.Directory.CreateDirectory(sentDir)
End If

End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles TextBox1.TextChanged

End Sub
End Class



'''''''''''''''''''''''''''''' Class1.vb
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Module SharedVar
Public srcPath As String
Public srcFilePath As String
Public destPath As String
Public NewFileName As String
Public err As Integer 'String '
End Module
 
F

Family Tree Mike

thomasc said:
Hello,
I wrote a simple FTP code which monitors a user-specified folder and,
if a new file is generated in the folder, uploads it to an FTP
server.

The program seems to be working on Visual Studio .NET 2003 IDE, and
the FTP server program says it has sucessfully received the file. But
I can't find the transferred file in the destination folder in FTP
server.

I have no idea why this happens.
I activated an FTP server on the same PC on which I run this program
on Visual Studio .NET 2003 IDE. Can it be why and I need to run the
program other than the PC where the FTP server is running?

Please take a look at my codes below and let me know what I'm doing
wrong.
Thank you mush in advance.

I don't have VS 2003, so I cannot test your code, though I think module ftp
as at least three issues in what you posted. You don't define srcFilePath
and destPath in the line below:

RC = FtpPutFile(INetConn, srcFilePath, destPath, FTP_TRANSFER_TYPE_BINARY,
0)

and I get an error "Expression is a value and therefore cannot be the target
of an assignment" on:

err = System.Runtime.InteropServices.Marshal.GetLastWin32Error()

Aside from those items, I think you should look at WebClient.UploadFile and
WebClient.DownloadFile in System.Net.

Also, I would think there is a chance that there is some time that the file
is created, and therefore your watcher creates it, but there is no data in
the file. The ftp server may say success for sending a zero byte file, but
not actually store it. I am not positive about that however.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top