none
FTP upload problem with invalid URI

    Question

  • I have used many different examples for ftp upload but the only one that made sense to me was this.

    http://msdn.microsoft.com/en-us/library/ms229715.aspx

     

    I have the code sorted out nicely and everything looks exactly the same but I get this exception every time I try to upload a text file to my ftp server.

    System.UriFormatException was unhandled

      Message=Invalid URI: The format of the URI could not be determined.

      Source=System

      StackTrace:

           at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)

           at System.Uri..ctor(String uriString)

           at Multitool.FtpClient.Upload(String filename) in C:\Users\Administrator\Documents\Visual Studio 2010\Projects\Multitool\Multitool\FtpClient.cs:line 37

           at Multitool.FtpClient.btnUpload_Click(Object sender, EventArgs e) in C:\Users\Administrator\Documents\Visual Studio 2010\Projects\Multitool\Multitool\FtpClient.cs:line 103

           at System.Windows.Forms.Control.OnClick(EventArgs e)

           at System.Windows.Forms.Button.OnClick(EventArgs e)

           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)

           at System.Windows.Forms.Control.WndProc(Message& m)

           at System.Windows.Forms.ButtonBase.WndProc(Message& m)

           at System.Windows.Forms.Button.WndProc(Message& m)

           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)

           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)

           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)

           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)

           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)

           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)

           at System.Windows.Forms.Application.Run(Form mainForm)

           at Multitool.Program.Main() in C:\Users\Administrator\Documents\Visual Studio 2010\Projects\Multitool\Multitool\Program.cs:line 18

           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)

           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)

           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)

           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

           at System.Threading.ThreadHelper.ThreadStart()

      InnerException: 

     

     

    This is my source code and as I said it looks perfect to me.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.IO;
    using System.Threading;
    
    namespace Multitool
    {
      public partial class FtpClient : Form
      {
        public String fileAddress;
        public String name;
    
        public FtpClient()
        {
          InitializeComponent();
        }
    
        private void FtpClient_Load(object sender, EventArgs e)
        {
    
        }
    
        private void Upload(string filename)
        {
          FileInfo fileInf = new FileInfo(filename);
          string uri = txtAddress.Text + "/" + fileInf.Name;
          FtpWebRequest reqFTP;
    
          // Create FtpWebRequest object from the Uri provided
          reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(txtAddress.Text + "/" + fileInf.Name));
    
          // Provide the WebPermission Credintials
          reqFTP.Credentials = new NetworkCredential(txtUsername.Text, txtPass.Text);
    
          // By default KeepAlive is true, where the control connection is not closed
          // after a command is executed.
          reqFTP.KeepAlive = false;
          
          // Specify the command to be executed.
          reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
          
          // Specify the data transfer type.
          reqFTP.UseBinary = true;
          
          // Notify the server about the size of the uploaded file
          reqFTP.ContentLength = fileInf.Length;
          
          // The buffer size is set to 2kb
          int buffLength = 2048;
          byte[] buff = new byte[buffLength];
          int contentLen;
          
          // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
          FileStream fs = fileInf.OpenRead();
          try
          {
            // Stream to which the file to be upload is written
            Stream strm = reqFTP.GetRequestStream();
          
            // Read from the file stream 2kb at a time
            contentLen = fs.Read(buff, 0, buffLength);
            
            // Till Stream content ends
            while (contentLen != 0)
            {
              // Write Content from the file stream to the FTP Upload Stream
              strm.Write(buff, 0, contentLen);
              contentLen = fs.Read(buff, 0, buffLength);
            }
            // Close the file stream and the Request Stream
            strm.Close();
            fs.Close();
          }
          catch (Exception ex)
          {
            MessageBox.Show(ex.Message, "Upload Error");
          }
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          OpenFileDialog Open = new OpenFileDialog();
    
          Open.Filter = "All Files (*.*)|*.*";
          Open.Title = "Select a file to upload.";
    
          if (Open.ShowDialog() == DialogResult.OK)
          {
            txtFile.Text = Open.FileName;
            fileAddress = txtFile.Text;
          }
        }
    
        private void btnUpload_Click(object sender, EventArgs e)
        {
          Upload(fileAddress);
        }
      }
    }
    
    

    Does anybody know why I get this error?

     

    Any help is greatly appreciated.

     

    Thanks in advance.

     

    Jai Brown.

     

     


    Thursday, July 22, 2010 2:45 PM

Answers


  • Can you try this??


    FileInfo fileInf = new FileInfo(filename);
    string uri = " ftp://" + ftpServerIP + " /" + fileInf.Name;
    FtpWebRequest reqFTP;


    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(
    " ftp://" + ftpServerIP + " /" + fileInf.Name));


    reqFTP.Credentials = new NetworkCredential(ftpUserID,
    ftpPassword);
    Thursday, July 22, 2010 3:43 PM
  • Ok I have tried using the code provided by Balu n but when I try to upload nothing happens.

    The GUI should freeze while the file is uploading shouldn't it?

    I have used the IP address and the URL of the site I use to upload to but nothing happens.

     

    Does anybody know what is going on there?

     

    This is my source now after using Balu n's code.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.IO;
    using System.Threading;
    
    namespace Multitool
    {
      public partial class FtpClient : Form
      {
        public String fileAddress;
        public String name;
    
        public FtpClient()
        {
          InitializeComponent();
        }
    
        private void FtpClient_Load(object sender, EventArgs e)
        {
    
        }
    
        private void Upload(string filename)
        {
          try
          {
            FileInfo fileInf = new FileInfo(filename);
    
            string uri = "ftp://" + txtAddress.Text + "/" + fileInf.Name;
    
            FtpWebRequest reqFTP;
    
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + txtAddress.Text + "/" + fileInf.Name));
    
            reqFTP.Credentials = new NetworkCredential(txtUsername.Text, txtPass.Text);
          }
          catch (Exception ex)
          {
            MessageBox.Show(ex.Message, "Upload Error");
          }
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          OpenFileDialog Open = new OpenFileDialog();
    
          Open.Filter = "All Files (*.*)|*.*";
          Open.Title = "Select a file to upload.";
    
          if (Open.ShowDialog() == DialogResult.OK)
          {
            txtFile.Text = Open.FileName;
            fileAddress = txtFile.Text;
          }
        }
    
        private void btnUpload_Click(object sender, EventArgs e)
        {
          Upload(fileAddress);
        }
      }
    }
    
    

    Does that look right?

    Friday, July 23, 2010 3:49 AM

All replies


  • Can you try this??


    FileInfo fileInf = new FileInfo(filename);
    string uri = " ftp://" + ftpServerIP + " /" + fileInf.Name;
    FtpWebRequest reqFTP;


    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(
    " ftp://" + ftpServerIP + " /" + fileInf.Name));


    reqFTP.Credentials = new NetworkCredential(ftpUserID,
    ftpPassword);
    Thursday, July 22, 2010 3:43 PM
  • Well, the error that you're getting is that the URI is formatted incorrectly, so the firs thing that I would do is print off the string that you're passing to the URI and see what it looks like; it may be a problem with the actual data.

    If you copy an example here we could see if there is a clear error with the format.

    Thursday, July 22, 2010 3:45 PM
  • Ok I have tried using the code provided by Balu n but when I try to upload nothing happens.

    The GUI should freeze while the file is uploading shouldn't it?

    I have used the IP address and the URL of the site I use to upload to but nothing happens.

     

    Does anybody know what is going on there?

     

    This is my source now after using Balu n's code.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.IO;
    using System.Threading;
    
    namespace Multitool
    {
      public partial class FtpClient : Form
      {
        public String fileAddress;
        public String name;
    
        public FtpClient()
        {
          InitializeComponent();
        }
    
        private void FtpClient_Load(object sender, EventArgs e)
        {
    
        }
    
        private void Upload(string filename)
        {
          try
          {
            FileInfo fileInf = new FileInfo(filename);
    
            string uri = "ftp://" + txtAddress.Text + "/" + fileInf.Name;
    
            FtpWebRequest reqFTP;
    
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + txtAddress.Text + "/" + fileInf.Name));
    
            reqFTP.Credentials = new NetworkCredential(txtUsername.Text, txtPass.Text);
          }
          catch (Exception ex)
          {
            MessageBox.Show(ex.Message, "Upload Error");
          }
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          OpenFileDialog Open = new OpenFileDialog();
    
          Open.Filter = "All Files (*.*)|*.*";
          Open.Title = "Select a file to upload.";
    
          if (Open.ShowDialog() == DialogResult.OK)
          {
            txtFile.Text = Open.FileName;
            fileAddress = txtFile.Text;
          }
        }
    
        private void btnUpload_Click(object sender, EventArgs e)
        {
          Upload(fileAddress);
        }
      }
    }
    
    

    Does that look right?

    Friday, July 23, 2010 3:49 AM
  •  

    The code looks good to me.

    Are you getting any exception?

     

     

    Sunday, July 25, 2010 2:23 PM