Results 1 to 3 of 3

Thread: FTP uploading

  1. #1

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    39

    FTP uploading

    Hello Everyone,

    This site is amazing. I have learnt so much reading the many posts.
    Does anyone have any suggestions as to what the best way of uploading a queue of files is?

    I am currently creating a FTPWebrequest to handle my uploading (the webrequest section of my function is below). At the moment my code loops through this webrequest section for each file - giving the file path of each file in the string "CompleteLocalPath". For each file it creates a webrequest and giving the required file path, uploading the file using a file stream, and then closing the stream. This works, but seems to take quite a long time.
    Can you recommend a more efficient way? Perhaps by using one webrequest but modifying the upload path?

    Thank you very much for your help

    Code:
     
    
    Dim fwr As FtpWebRequest = FtpWebRequest.Create("ftp://ftp-server.com/directory/"+path.getfilename(completelocalpath))
    
    fwr.Credentials = New NetworkCredential("username", "password")
    fwr.Method = WebRequestMethods.Ftp.UploadFile
    
    Dim requestStream As FileStream = File.OpenRead(CompleteLocalPath)
    
    Dim buffer(requestStream.Length) As Byte
    requestStream.Read(buffer, 0, buffer.Length)
    requestStream.Close()
    requestStream = Nothing
    fwr.GetRequestStream.Write(buffer, 0, buffer.Length)
    fwr = Nothing

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: FTP uploading

    From the looks of things you don't really need th extra control provided by the FtpWebRequest, so it would be simpler to use a WebClient and call UploadFile or even just call My.Computer.Network.UploadFile.

    To upload multiple files in parallel, you could use the ThreadPool to upload each file on a different thread, calling UploadFile for each one. Alternatively, you could use the WebClient and call UploadFileAsync, which will inherently upload on a secondary thread. Note that, if you use a WebClient, you will need one for each file.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    May 2010
    Posts
    39

    Re: FTP uploading

    Thanks a lot,
    I'm going with the multi-threading and it is almost working I think. I will post the code when I have it correctly

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width