-
June 16th, 2016, 03:46 PM
#1
how can i use one connection to upload multiple files in vb.net
i've this application that reads multiple files from a folder and upload them to the server..it works fine but when it wants to upload each file, it creates a connection, upload the file and closes the connection. it does the same thing for all the files over and over again. i want a situation where it creates just one connection, upload all the files and close the connection. please any help will be appreciated, i searched everywhere but couldn't find any help. below is my code
Code:
Imports System.IO
Imports System.Net
Public Class DemoFTPServerApp
Dim _Filename As String
Dim _UploadPath As String
Dim f As String
Public Sub LoadFiles(_User As String, _Password As String, _Path As String)
Dim _MyArraylist As New ArrayList
Dim FolderPath As String = "C:\Users\Desktop\files"
Dim finfo As New DirectoryInfo(FolderPath)
For Each fi In finfo.GetFiles("*.txt")
_MyArraylist.Add(fi.FullName) 'full path only
_Filename = fi.FullName
f = fi.ToString()
_UploadPath = _Path & f
Try
Dim request As FtpWebRequest = DirectCast(WebRequest.Create(New Uri(_UploadPath)), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.UploadFile
request.Credentials = New NetworkCredential(_User, _Password)
request.UseBinary = True
request.UsePassive = False
request.KeepAlive = True
request.ConnectionGroupName = "company name"
request.ServicePoint.ConnectionLimit = 4
request.ServicePoint.CloseConnectionGroup("company name")
Dim buffer(1023) As Byte
Dim bytesIn As Long = 1
Dim filepath As System.IO.FileInfo = New System.IO.FileInfo(_Filename)
Dim _FileStream As System.IO.FileStream = filepath.OpenRead()
Dim _Stream As System.IO.Stream = request.GetRequestStream
Do Until bytesIn < 1
bytesIn = _FileStream.Read(buffer, 0, 1024)
If bytesIn > 0 Then
_Stream.Write(buffer, 0, bytesIn)
End If
Loop
_Stream.Close()
_Stream.Dispose()
_FileStream.Close()
_FileStream.Dispose()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Next
MessageBox.Show("File Succesfully uploaded!")
End Sub
Private Sub btnUploadFile_Click(sender As Object, e As EventArgs) Handles btnUploadFile.Click
LoadFiles("username", "password", "ftp://ftpsite.com/")
End Sub
Tags for this Thread
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
|