Upload File via FTP (VB.NET 2005)-VBForums
Page 1 of 2 12 LastLast
Results 1 to 40 of 47

Thread: Upload File via FTP (VB.NET 2005)

  1. #1

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,358

    Upload File via FTP (VB.NET 2005)

    this code will let you upload a file on an ftp server. working on vb.net 2005
    i hope it will help u

    Upload via FTP Code:
    1. ' set up request...
    2.         Dim clsRequest As System.Net.FtpWebRequest = _
    3.             DirectCast(System.Net.WebRequest.Create("ftp://ftp.myserver.com/test.txt"), System.Net.FtpWebRequest)
    4.         clsRequest.Credentials = New System.Net.NetworkCredential("myusername", "mypassword")
    5.         clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
    6.  
    7.         ' read in file...
    8.         Dim bFile() As Byte = System.IO.File.ReadAllBytes("C:\Temp\test.txt")
    9.  
    10.         ' upload file...
    11.         Dim clsStream As System.IO.Stream = _
    12.             clsRequest.GetRequestStream()
    13.         clsStream.Write(bFile, 0, bFile.Length)
    14.         clsStream.Close()
    15.         clsStream.Dispose()

    Enjoy

  2. #2
    New Member
    Join Date
    Oct 2007
    Posts
    1

    Re: Upload File via FTP (VB.NET 2005)


    how do i create a new directory on the server and then upload a file?
    thanks

  3. #3
    Lively Member
    Join Date
    Feb 2008
    Posts
    91

    Re: Upload File via FTP (VB.NET 2005)

    Thank you very much, i was suprised how it worked!
    Is there anyway to DOWNLOAD a file from the Ftp? that would be awesome.

  4. #4
    Member
    Join Date
    Mar 2008
    Posts
    48

    Re: Upload File via FTP (VB.NET 2005)

    This is JUST what I needed for my application! it works perfect! Thankyou very much

  5. #5
    Addicted Member kake_fisk's Avatar
    Join Date
    May 2008
    Posts
    192

    Re: Upload File via FTP (VB.NET 2005)

    But is it secure?

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Upload File via FTP (VB.NET 2005)

    Quote Originally Posted by kake_fisk
    But is it secure?
    FTP is, by default, not encrypted. So no.
    ---My Flickr photo (mostly screenshots these days!) stream. Have a look!

    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    TCP client/server connection | Retrieving the EventHandler for any Event by code.
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7
    Addicted Member kake_fisk's Avatar
    Join Date
    May 2008
    Posts
    192

    Re: Upload File via FTP (VB.NET 2005)

    Okay :\
    But can't you just put in clsrequest.sslenabled = true ?

  8. #8
    New Member Protege's Avatar
    Join Date
    Jul 2008
    Location
    Southwest England
    Posts
    2

    Re: Upload File via FTP (VB.NET 2005)

    "The requested URI is invalid for this FTP command."

    @ line ->
    Code:
    Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
    If you could help, be great - thank you.

  9. #9
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Upload File via FTP (VB.NET 2005)

    How are you creating 'clsRequest'?
    ---My Flickr photo (mostly screenshots these days!) stream. Have a look!

    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    TCP client/server connection | Retrieving the EventHandler for any Event by code.
    Me on.. BitBucket, Google Code, Github (pretty empty)

  10. #10
    New Member Protege's Avatar
    Join Date
    Jul 2008
    Location
    Southwest England
    Posts
    2

    Re: Upload File via FTP (VB.NET 2005)

    Code:
    Imports System.Net
    Module Module1
    
        Sub Main()
            Dim clsRequest As System.Net.FtpWebRequest = _
                DirectCast(System.Net.WebRequest.Create("ftp://ftp.interstudios.co.uk"), System.Net.FtpWebRequest)
            clsRequest.Credentials = New System.Net.NetworkCredential("uploadusr", "uploadpw")
            clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
    
            ' read in file...
            Dim bFile() As Byte = System.IO.File.ReadAllBytes("C:\test.txt")
    
            ' upload file...
            Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
            clsStream.Write(bFile, 0, bFile.Length)
            clsStream.Close()
            clsStream.Dispose()
        End Sub
    
    End Module
    I haven't really changed anything as of yet.

  11. #11
    New Member
    Join Date
    Jul 2008
    Posts
    1

    Re: Upload File via FTP (VB.NET 2005)

    hi, thx for hint

    i was so confused

    However, what if the server has a proxy that blocks!!!

  12. #12
    New Member
    Join Date
    Jul 2008
    Posts
    2

    Re: Upload File via FTP (VB.NET 2005)

    I keep getting an error message on the first line which says: "Unable to cast object of type 'System.Net.FileWebRequest' to type 'System.Net.FtpWebRequest'." Does anyone have any idea what's causing this? Thanks!

  13. #13

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,358

    Re: Upload File via FTP (VB.NET 2005)

    make sureyou have this code at the top of all codes

    vb Code:
    1. Imports System.Net

  14. #14
    New Member
    Join Date
    Jul 2008
    Posts
    2

    Re: Upload File via FTP (VB.NET 2005)

    Yes, I have that at the top. I also tried using fully qualified names such as:

    Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("xxxxxxxxxx"), System.Net.FtpWebRequest)

    Still, no luck. But I've seen the original code that was posted on multiple websites, so it must work. I'm using VB.Net 2008, and I've tried it using the 2.0 and 3.5 Framework. I'm out of ideas at this point.

  15. #15
    New Member
    Join Date
    Aug 2008
    Posts
    1

    Re: Upload File via FTP (VB.NET 2005)

    The code given in the first post works perfectly fine when running locally:

    Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://ftp.website.com/Newsletters/" & DateTime.Now.ToString("MMMM-yyyy") & " Newsletter.doc"), System.Net.FtpWebRequest)
    clsRequest.Credentials = New System.Net.NetworkCredential("user", "password")
    clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
    'read in file...

    Dim bFile() As Byte = System.IO.File.ReadAllBytes(upControl.PostedFile.FileName)

    'upload the file
    Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
    clsStream.Write(bFile, 0, bFile.Length)
    clsStream.Close()
    clsStream.Dispose()

    ======


    However when I upload my .aspx pages and test the uploading of word documents live (domain hosted by go daddy), the file never gets posted to my Newsletters directory....

    Does anyone see something I am doing wrong?

    Thanks for any help in advance.

  16. #16

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,358

    Re: Upload File via FTP (VB.NET 2005)

    well, it works fine with vb.net but i didn't try with asp.net

  17. #17
    New Member
    Join Date
    Aug 2008
    Posts
    3

    Re: Upload File via FTP (VB.NET 2005)

    The reason why you cannot use that in asp.net is because asp.net does not use the same functionality. I recommend using asp.net's FileUpload control. It is a hell of a lot easier.

  18. #18
    New Member
    Join Date
    Feb 2005
    Posts
    6

    Re: Upload File via FTP (VB.NET 2005)

    Just a quick question about this.
    Does this send the file over in binary mode?

  19. #19

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,358

    Re: Upload File via FTP (VB.NET 2005)

    i think you can adjust that

  20. #20
    New Member
    Join Date
    Aug 2008
    Posts
    3

    Re: Upload File via FTP (VB.NET 2005)

    The asp.net file upload control is fully customizable. If you check MSDN, under FileUpload Control, you will see a full list of Methods and all the class details.

  21. #21
    New Member
    Join Date
    Sep 2008
    Posts
    2

    Re: Upload File via FTP (VB.NET 2005)

    This code works great for me in .Net 2005, thanks.

    But is there any way to monitor upload progress?

  22. #22
    New Member
    Join Date
    Aug 2008
    Posts
    3

    Smile Re: Upload File via FTP (VB.NET 2005)

    Simple enough, with the fileupload control, grab the filesize using, I think, fileupload(whatever the name for your control is, if it was for pics, I would 'id' it as 'fileupload_pics'. so use something like:
    //
    Dim filesize = fileupload_pics.filebytes
    //

    the .filebytes member of the fileupload control gets the filesize from the selected file. Be sure to input logic to make sure that a file was selected and exists.
    From there, you want to monitor how much data is uploaded and compare. I think Javascript has native support for monitoring how much data is being uploaded. .Net does as well, but its server side and you wouldn't want to post back everytime there was an update, even via ajax as its more cumbersome and still does a partial postback regardless. The best way is to use client side code to just update. Have javascript monitor the current amount uploaded after the click event to the full file size by passing the filebytes variable to the Javascript. For the progress bar, a good idea is to create a div with, say a width of 100px and a border. Then use javascript to put in a background. You can even use a gif and use a third div and subtract that one, so you have a nice flowing animation as well, but back on track... Make the script loop through continuously, maybe, at intervals of 25-50ms. Every 1/100th % of the file that is uploaded on comparision, add 1 pixel. You can scale the div to whatever pixel width you need. I was just using a 100px div as an example.

    Sound good?

  23. #23
    New Member
    Join Date
    Sep 2008
    Posts
    2

    Re: Upload File via FTP (VB.NET 2005)

    Hi

    I did it by altering the "upload file" part of the original code to do the following:

    Code:
                ' upload file...
                Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
                intChunk = bFile.Length / 100
                pbUpload.Maximum = 100
                For I As Integer = 1 To 100
                    If intOffset + intChunk > bFile.Length Then intChunk = bFile.Length - intOffset
                    clsStream.Write(bFile, intOffset, intChunk)
                    intOffset += intChunk
                    pbUpload.Value = I
                Next
                clsStream.Close()
                clsStream.Dispose()
    Which seems to work hundreds for me.

  24. #24

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,358

    Re: Upload File via FTP (VB.NET 2005)

    , please rate if it helped

    thx

  25. #25
    Lively Member
    Join Date
    Sep 2007
    Posts
    73

    Re: Upload File via FTP (VB.NET 2005)

    I get this error when I try to run it:

    The requested URI is invalid for this FTP command.

    any ideas on how to fix it?
    Last edited by want a pie; Sep 28th, 2008 at 12:59 AM.

  26. #26

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,358

    Re: Upload File via FTP (VB.NET 2005)

    Hi,

    Can you post ur code please

  27. #27
    Lively Member
    Join Date
    Sep 2007
    Posts
    73

    Re: Upload File via FTP (VB.NET 2005)

    just used the code u posted

  28. #28
    New Member
    Join Date
    Oct 2008
    Posts
    1

    Re: Upload File via FTP (VB.NET 2005)

    I put this code in my progect but when I did a little pass large files and entered the program as a whole hangs situation is not responding to end the transfer a large file.
    It was the Cshapradti the rest of the software to form2.
    What should I do?
    tnx`

  29. #29
    New Member
    Join Date
    Jan 2009
    Posts
    1

    Unhappy Re: Upload File via FTP (VB.NET 2005)

    hi,
    I'm getting a reply from the server saying that it is entering Passive mode.
    the reply is captured as an exception.
    Is there a way around it?
    Can I ignore the server response? can I change something in the code to make sure the server will not use passive mode? Anything else I can do?

    Gil

  30. #30
    New Member
    Join Date
    Apr 2009
    Posts
    2

    Re: Upload File via FTP (VB.NET 2005)

    The code below is quite impressive and worked first time for me (thanks met0555).
    I have a simple text file which I can successfully upload to my Web Server. However, on viewing the text either directly on the Web Server, or by viewing the source in a Web Browser, I have a double line break in place of a single one and the page goes on for ever! The server I'm uploading to is: Apache Version:1.3.41 (Unix).

    I don't have any issues when I upload with CuteFtp. I've tried other Web Servers and run into the same problem. Any ideas?


    Quote Originally Posted by met0555 View Post
    this code will let you upload a file on an ftp server. working on vb.net 2005
    i hope it will help u

    Upload via FTP Code:
    1. ' set up request...
    2.         Dim clsRequest As System.Net.FtpWebRequest = _
    3.             DirectCast(System.Net.WebRequest.Create("ftp://ftp.myserver.com/test.txt"), System.Net.FtpWebRequest)
    4.         clsRequest.Credentials = New System.Net.NetworkCredential("myusername", "mypassword")
    5.         clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
    6.  
    7.         ' read in file...
    8.         Dim bFile() As Byte = System.IO.File.ReadAllBytes("C:\Temp\test.txt")
    9.  
    10.         ' upload file...
    11.         Dim clsStream As System.IO.Stream = _
    12.             clsRequest.GetRequestStream()
    13.         clsStream.Write(bFile, 0, bFile.Length)
    14.         clsStream.Close()
    15.         clsStream.Dispose()

    Enjoy

  31. #31
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    413

    Re: Upload File via FTP (VB.NET 2005)

    try
    clsRequest.UseBinary = false (default is true, False is for text)
    Visual Studio .NET 2005/.NET Framework 2.0

  32. #32
    New Member
    Join Date
    Apr 2009
    Posts
    2

    Re: Upload File via FTP (VB.NET 2005)

    Thanks for that, but I solved the problem by creating my text file using chr(10) instead of vbnewline. Must be something to do with ASCII character set.

  33. #33
    Junior Member
    Join Date
    Mar 2009
    Posts
    27

    Question Re: Upload File via FTP (VB.NET 2005)

    nice post!! guyss

    is there any way to upload image over internet via .net cf thru ftp??

  34. #34
    New Member TWDO's Avatar
    Join Date
    Nov 2007
    Location
    Brooksville, FL
    Posts
    3

    Re: Upload File via FTP (VB.NET 2005)

    Quote Originally Posted by eyeconeeyecone View Post
    nice post!! guyss

    is there any way to upload image over internet via .net cf thru ftp??

    Download the freeware code at www.aspDotNetSoftware.com for the application called FTP Backup. Within that source code, you can see how to upload files to a remote FTP server with ease. It even shows how to easily do it with multiple threads. You need to create an account before you can download, but it is WELL worth it. Trust me.

    - Thanks
    - John Johnson
    - Sr. Software Engineer
    John Johnson
    Sr. Software Engineer
    asp.netSoftware :: Software for Developers

  35. #35
    New Member
    Join Date
    Aug 2008
    Posts
    2

    Re: Upload File via FTP (VB.NET 2005)

    How would you add in or increase buffer size to 2048?

  36. #36
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    267

    Re: Upload File via FTP (VB.NET 2005)

    this scripts works perfectly .. but what if i want to upload a second file without opening en closing the FTP connection?

  37. #37
    New Member
    Join Date
    Mar 2010
    Posts
    4

    Re: Upload File via FTP (VB.NET 2005)

    Hello to all,

    i used the above code and i am happy to inform you that it works. But i have a problem. I want to make an application that uploads screenshots of my desktop to my server via ftp.

    This is the code i use:
    Code:
    Public Class Form1
        Dim SC As New ScreenShot.ScreenCapture
        Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("*"), System.Net.FtpWebRequest)
        Dim bFile() As Byte
        Dim clsStream As System.IO.Stream
    
        Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            clsRequest.Credentials = New System.Net.NetworkCredential("*", "*")
            clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            SC.CaptureScreenToFile("c:\desktop2.jpg", Imaging.ImageFormat.Jpeg)
            bFile = System.IO.File.ReadAllBytes("c:\desktop2.jpg")
            clsStream = clsRequest.GetRequestStream()
            clsStream.Write(bFile, 0, bFile.Length)
            clsStream.Close()
            clsStream.Dispose()
        End Sub
    End Class
    When i run the application and press button1 the image of my desktop is being saved to c:\desktop2.jpg and is being uploaded to the server. But when i click the button for second time (or third, or....) this error is produced at line "clsStream = clsRequest.GetRequestStream()":
    "Cannot re-call BeginGetRequestStream/BeginGetResponse while a previous call is still in progress."

    So i tried to make a modification and i put "clsStream = clsRequest.GetRequestStream()" out of the button1_Click() and paste it at form1_Load().

    When i try to run this, it works for the first button1 click but at the second a new error comes at line "clsStream.Write(bFile, 0, bFile.Length)": Cannot access a disposed object.

    So i decided to delete the two lines that close and dispose the object (clsStream.Close(), clsStream.Dispose()). I run this again, first click everything is right, second click no error produced but although the new desktop image is being saved to c:, the file i found to my server is the old one but (almost) double in size. If i press for third time the button, the local image is refreshed but the image on server is the old one but (almost) with triple size of the original.

    What's the problem? I think that i want to do an easy job but i can't. Just press the button upload an image via ftp to server. Press again the button, upload an image to server and so on.

    Thank in advance,
    Thanasis

  38. #38
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    788

    Re: Upload File via FTP (VB.NET 2005)

    If trying this locally what gets entered in this part
    DirectCast(System.Net.WebRequest.Create("ftp://ftp.myserver.com/test.txt"),

  39. #39
    Lively Member
    Join Date
    Mar 2010
    Posts
    70

    Re: Upload File via FTP (VB.NET 2005)

    Hello guys,
    I already known this feature, but I would like to know if there's any chance of encrypting the password, so if someone try to debug my code after it has been compiled, he cannot see the real FTP password.

    Suggestions?

  40. #40
    Lively Member
    Join Date
    Feb 2010
    Posts
    106

    Re: Upload File via FTP (VB.NET 2005)

    i get an error on this line(and I am using the code you posted):

    vb Code:
    1. Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()

Page 1 of 2 12 LastLast

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.