Click here to Skip to main content
15,896,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


VB
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


What I have tried:

This code is working fine but i need a little help on how to modify it.
i tried to take the FTP connection out of the loop but it throws an ArgumentNullException error which i expected.
Posted
Updated 16-Jun-16 13:41pm

1 solution

Before considering any specialized controls, consider the simplest possibility: a Web form and the multiple attribute of the input control of the type "file". Please see:
Using files from web applications.
This is how it may look:
HTML
<form action="someScript.asp">
  <!-- ... -->
  Select images: <input type="file" name="fileSet" multiple="true">
  <input type="submit">
</form>

Such form will send HTTP request to the server side; with all the files entered. The real effect of the attribute multiple is that the file dialog box will be shown in multi-select mode, so the user can chose more than one file. All the file data will be sent in the HTTP request to the server side where you can handle it.
See also:
HTML forms guide — Web developer guides,
<form> — HTML.

—SA
 
Share this answer
 
v2
Comments
koriewhite 16-Jun-16 15:44pm    
Thanks SA for your contribution.. sorry my application is a windows application and not web application
Sergey Alexandrovich Kryukov 16-Jun-16 19:34pm    
And then... oh, then this is not a problem at all.

You can use regular file open dialog and allow the user to select any number of files. Then you can compose one single HTTP request ("post") and write all the data you need to transfer. The adequate class for this activity is System.Net.HttpWebRequest:
https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest%28v=vs.110%29.aspx.

And your server side should be ready for accepting several files. If you say that your application is not a Web application, it may mean that you still have some Web application/site, but it's not yours, so you cannot modify it. If so, you still can only do what that Web application expects. If it can accept several files in one post, you can do such post. That's all.

Will you accept my answer formally now?

—SA
koriewhite 17-Jun-16 15:06pm    
Thanks.. i've another application that uses file open dialog to select the files and it works perfectly but this application reads files from a folder without any GUI interaction to select files. i
thank you but this can't help me solve my problem.
Sergey Alexandrovich Kryukov 17-Jun-16 15:11pm    
Why not? How the issues with the site and connection is related with your different application? Upload whatever you want; I mentioned the dialog only for illustration, as the analogy with browser behavior.

This is all you really need (my comment above), but it depends on what you have on the site's server side.
My advice either solves your problem, or it might be unsolvable without changing of the site's code...

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900