Results 1 to 4 of 4

Thread: FTP Uploader

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    5

    FTP Uploader

    I am trying to create a console application that will upload an updated .html file to a website each time I run it, I have found previous codes that download the files but I want to upload instead.

    So basically I am trying to create an FTP program (but only one that uploads) in a console application

    Does anyone know can this can be done?

    Thanks

  2. #2
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: FTP Uploader

    Try this. You'll have to add error checking yourself.

    Code:
    FTPUpload.exe /f "ftp://somehost.com/FileToUpload.pdf" /n "C:\FileToUpload.pdf" /u "username" /p "password"
    vb.net Code:
    1. Imports System.Net
    2. Imports System.Threading
    3.  
    4. Module FTPUpload
    5.  
    6.     Dim FTP As New WebClient
    7.     Dim ManualEvent As ManualResetEvent
    8.     Dim UploadTime As New Stopwatch
    9.  
    10.     Sub Main(ByVal args As String())
    11.         Dim RemotePath As String = String.Empty
    12.         Dim LocalFile As String = String.Empty
    13.         Dim Username As String = String.Empty
    14.         Dim Password As String = String.Empty
    15.  
    16.         Dim i As Integer
    17.         For i = 0 To args.Length - 1
    18.             Select Case args(i)
    19.                 Case "/f"
    20.                     i += 1
    21.                     RemotePath = args(i)
    22.                 Case "/n"
    23.                     i += 1
    24.                     LocalFile = args(i)
    25.                 Case "/u"
    26.                     i += 1
    27.                     Username = args(i)
    28.                 Case "/p"
    29.                     i += 1
    30.                     Password = args(i)
    31.                 Case Else
    32.                     Console.WriteLine("Invalid parameters!")
    33.                     Console.ReadKey()
    34.                     Return
    35.             End Select
    36.         Next
    37.  
    38.         Console.WriteLine("")
    39.         Console.WriteLine("Uploading file to the server. Please wait...")
    40.         Console.WriteLine("")
    41.  
    42.         AddHandler FTP.UploadProgressChanged, AddressOf UploadProgressChanged
    43.         AddHandler FTP.UploadFileCompleted, AddressOf UploadFileCompleted
    44.         FTP.Credentials = New NetworkCredential(Username, Password)
    45.         FTP.UploadFileAsync(New Uri(RemotePath), LocalFile)
    46.  
    47.         UploadTime.Start()
    48.  
    49.         ManualEvent = New ManualResetEvent(False)
    50.         ManualEvent.WaitOne()
    51.     End Sub
    52.  
    53.     Private Sub UploadProgressChanged(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs)
    54.         Dim FixedProgress As Integer = If(e.ProgressPercentage <> 100, e.ProgressPercentage * 2, e.ProgressPercentage)
    55.         Console.Write("{0}Upload Progress: {1} bytes / {2} bytes - {3}%", vbCr, e.BytesSent, e.TotalBytesToSend, FixedProgress)
    56.     End Sub
    57.  
    58.     Private Sub UploadFileCompleted(ByVal sender As Object, ByVal e As UploadFileCompletedEventArgs)
    59.         Console.WriteLine("")
    60.  
    61.         If e.Error IsNot Nothing Then
    62.             Console.WriteLine(e.Error.Message.ToString)
    63.         Else
    64.             Console.WriteLine("Upload successfully finished in {0} seconds!", UploadTime.Elapsed.TotalSeconds)
    65.         End If
    66.  
    67.         Console.WriteLine("")
    68.         Console.WriteLine("Press any key to close this window...")
    69.         Console.ReadKey()
    70.         UploadTime.Stop()
    71.         ManualEvent.Set()
    72.     End Sub
    73. End Module

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    5

    Re: FTP Uploader

    Yes!! that works thanks very much, been searching around to find out how to do it for a while now.
    Thanks

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    5

    Re: FTP Uploader

    Quote Originally Posted by Chris001 View Post
    Sub Main(ByVal args As String())
    That code works on its own great, but now I am trying to implement it into another program but have a problem, I have changed the name of that sub to "write" but when I try to then make the program go to that sub from somewhere else by doing write() I have to pass it the thing for the args but I'm not sure where the value for args came from? So how can I run that sub from somewhere else?
    Hope that makes sense
    Thanks

    Update:
    Actually don't worry, I have figured it out
    Last edited by nickbu1; Dec 18th, 2011 at 04:44 PM.

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
  •  



Click Here to Expand Forum to Full Width