|
-
Dec 6th, 2011, 04:01 AM
#1
New Member
Re: Upload File via FTP (VB.NET 2005)
Hi all,
for those who are interested, here is some sample (working) VB 2005 code to upload multiple files via FTP. the code includes the following extras. will check if internet connection is active, upload each file, and then move it to an archive directory. Hope this helps
Imports System
Imports System.IO
Imports System.Net
Imports System.Runtime.InteropServices
Imports System.Text
Imports OpenNETCF.Net
Imports OpenNETCF.Net.Ftp
Public Class Form1
Public Function IsConnectionAvailable() As Boolean
' Returns True if connection is available
' Replace www.yoursite.com with a site that
' is guaranteed to be online - perhaps your
' corporate site, or microsoft.com
Dim objUrl As New System.Uri("http://(FTP server address)") ' without ()
' Setup WebRequest
Dim objWebReq As System.Net.WebRequest
objWebReq = System.Net.WebRequest.Create(objUrl)
Dim objResp As System.Net.WebResponse
Try
' Attempt to get response and return True
objResp = objWebReq.GetResponse
objResp.Close()
objWebReq = Nothing
Return True
Catch ex As Exception
' Error, exit and return False
'objResp.Close()
objWebReq = Nothing
Return False
End Try
End Function
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If IsConnectionAvailable() = True Then
Label4.Text = "Internet connection Active"
Label6.Text = "Uploading Files"
'' FTP Server
Dim sFTPServer As String = "ftp://(FTP server address)" ' without ()
'' FTP User Account
Dim sUserName As String = "(username)" ' without ()
''FTP sPassword
Dim sPassword As String = "(password)" ' without ()
For Each sFilePath As String In IO.Directory.GetFiles("c:\folder\")
Try
Dim FtpRequestCreator As New FtpRequestCreator
WebRequest.RegisterPrefix("ftp:", FtpRequestCreator)
Dim oFtpWebRequest As New FtpWebRequest(New Uri(sFTPServer))
oFtpWebRequest.Credentials = New NetworkCredential(sUserName, sPassword)
' Getting the Request stream
Dim oFtpRequestStream As Stream = oFtpWebRequest.GetRequestStream()
Dim oStreamReader As New StreamReader(oFtpRequestStream)
'Just ignore the result, but read it.
Dim oReader As String = oStreamReader.ReadToEnd
'Open the input file. If the file does not exist, it's an error.
Dim oFileStream As New FileStream(sFilePath, FileMode.Open)
'Create the reader for the local file data.
Dim oFileReader As BinaryReader = New BinaryReader(oFileStream)
'Opening the data connection, this must be done before we issue the command.
Dim oFtpResponseStream As Stream = oFtpWebRequest.GetResponse().GetResponseStream()
Dim oDataWriter As New BinaryWriter(oFtpResponseStream)
'Prepare to send commands to the server.
Dim cmdWriter As New StreamWriter(oFtpRequestStream)
'Set transfer type to IMAGE (binary).
cmdWriter.Write("TYPE I\r\n" & vbCrLf)
cmdWriter.Flush()
'Reading the request output
Dim oStreamReader2 As New StreamReader(oFtpRequestStream)
oReader = oStreamReader.ReadToEnd()
'Reading the request output
Dim sReader As String
sReader = oStreamReader2.ReadToEnd()
'Write the command to the request stream.
cmdWriter.Write("STOR " + Path.GetFileName(sFilePath) + vbCrLf)
cmdWriter.Flush()
sReader = oStreamReader2.ReadToEnd()
'Allocate buffer for the data, which will be written in blocks.
Dim bytes As Integer
Dim bufsize As Integer = 4096
Dim buffer(bufsize) As Byte
While True
bytes = oFileReader.Read(buffer, 0, bufsize)
oDataWriter.Write(buffer, 0, bytes)
If bytes <= 0 Then
Exit While
End If
End While
oFileReader.Close()
oFileStream.Close()
oDataWriter.Close()
oStreamReader.Close()
Dim foundFileInfo As New System.IO.FileInfo(sFilePath)
IO.File.Move(sFilePath, "C:\Folder\archive\" & foundFileInfo.Name)
'Label6.Text = "Uploading Complete"
Catch ex As Exception
sFilePath = Nothing
End Try
System.Threading.Thread.Sleep(5000)
Next
Else
Label4.Text = "No internet connection"
Label6.Text = "No FTP in progress"
End If
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|