Kraxed
Kraxed

Reputation: 350

Send File to my website

I am trying to send a file through FTP in VB.NET.

I have 3 labels with the server password and user information and a textbox with the file location called txtFile and my Textbox 1 has the new file name.

I click send and it doesn't come up in my website?

My.Computer.Network.UploadFile(txtFile.Text, 
                               ServLabel.Text & TextBox1.Text, 
                               PassLabel.Text, 
                               UserLabel.Text)

Upvotes: 0

Views: 304

Answers (3)

Johny
Johny

Reputation: 19

using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;

namespace Examples.System.Net
{
    public class WebRequestGetExample
    {
        public static async Task Main()
        {
            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
            request.Method = WebRequestMethods.Ftp.UploadFile;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential("anonymous", "[email protected]");

            // Copy the contents of the file to the request stream.
            using (FileStream fileStream = File.Open("testfile.txt", FileMode.Open, FileAccess.Read))
            {
                using (Stream requestStream = request.GetRequestStream())
                {
                    await fileStream.CopyToAsync(requestStream);
                    using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
                    {
                        Console.WriteLine($"Upload File Complete, status {response.StatusDescription}");
                    }
                }
           }
        }
    }
}

Upvotes: 1

PGallagher
PGallagher

Reputation: 3113

According to; http://msdn.microsoft.com/en-us/library/dfkdh7eb(v=vs.90).aspx

You have your Username and Password swapped;

Public Sub UploadFile( _
   ByVal sourceFileName As String, _
   ByVal address As String, _
   ByVal userName As String, _
   ByVal password As String _
)

and should be doing;

My.Computer.Network.UploadFile(txtFile.Text, 
                               ServLabel.Text & TextBox1.Text, 
                               UserLabel.Text,
                               PassLabel.Text)

Also, make sure you have the necessary Path Separators between your Server Address, and the file name.

Upvotes: 2

alu
alu

Reputation: 759

You could use the FtpWebRequest class. Here is an example: http://msdn.microsoft.com/en-us/library/ms229715.aspx

Upvotes: 1

Related Questions