-2

I have tried this code for uploading files to ftp server but this error comes up.I dont where am I going wrong. I tried various ways changing my ftpurl format but still no luck.

Code:

 private void button1_Click_1(object sender, EventArgs e)
        {
            UploadFileToFTP(sourcefilepath);
        }
        private static void UploadFileToFTP(string source)
        {
            String sourcefilepath = "C:/Users/Desktop/LUVS/*.xml";  
            String ftpurl = "100.100.0.35"; // e.g. fake IDs
            String ftpusername = "ftp"; // e.g. fake username
            String ftppassword = "1now"; // e.g. fake password


            try
            {
                string filename = Path.GetFileName(source);
                string ftpfullpath = ftpurl;
                FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
                ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);

                ftp.KeepAlive = true;
                ftp.UseBinary = true;
                ftp.Method = WebRequestMethods.Ftp.UploadFile;

                FileStream fs = File.OpenRead(source);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                fs.Close();

                Stream ftpstream = ftp.GetRequestStream();
                ftpstream.Write(buffer, 0, buffer.Length);
                ftpstream.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
Calum
  • 1,889
  • 2
  • 18
  • 36
shruthi
  • 71
  • 1
  • 2
  • 11
  • Which line throws the error and what is the runtime value of the erroneous URI on that line? (Hint: It would be a lot easier to determine such things if you weren't throwing away your stack trace. Your exception handler isn't doing anything, but is throwing away useful information. Just get rid of the exception handler entirely in its current form and let the original exception be thrown.) – David Jun 13 '14 at 09:51
  • Hi David, its giving me the error at this line `FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);` – shruthi Jun 13 '14 at 09:58
  • The error is pretty clear: "100.100.0.35" is not an URI. You're missing a scheme, in this case `ftp://`. – CodeCaster Jun 13 '14 at 10:10
  • I tried this it gives me - Illegal characters in path exception on this line `FileStream fs = File.OpenRead(source);`- – shruthi Jun 13 '14 at 10:15
  • One request is for one file. You can't use `*`, you'll have to loop through all files by code. See [Get all files and directories in specific path fast](http://stackoverflow.com/questions/6061957/get-all-files-and-directories-in-specific-path-fast). – CodeCaster Jun 13 '14 at 10:19
  • I have tried this with one file also same error – shruthi Jun 13 '14 at 10:25
  • C:/Users/IT-Administrator/Desktop/LUVS/a.xml – shruthi Jun 13 '14 at 10:25

2 Answers2

3

The error is in ftp url. You have not included the file name. Write it like this:

    private static void UploadFileToFTP(string source)
    {
        String sourcefilepath = "C:\\Users\\Desktop\\LUVS\\a.xml";  
        String ftpurl = "100.100.0.35"; // e.g. fake IDs
        String ftpusername = "ftp"; // e.g. fake username
        String ftppassword = "1now"; // e.g. fake password


        try
        {
            string filename = Path.GetFileName(sourcefilepath);
            string ftpfullpath = "ftp://" + ftpurl + "/" + filename ;
            FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
            ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);

            ftp.KeepAlive = true;
            ftp.UseBinary = true;
            ftp.Method = WebRequestMethods.Ftp.UploadFile;

            FileStream fs = File.OpenRead(sourcefilepath); // here, use sourcefilepath insted of source.
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            fs.Close();

            Stream ftpstream = ftp.GetRequestStream();
            ftpstream.Write(buffer, 0, buffer.Length);
            ftpstream.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
Ricky
  • 2,323
  • 6
  • 22
  • 22
2

The Create method appears to be on WebRequest, not on FtpWebRequest. And WebRequest needs to determine from the URI which child object to create (in this case FtpWebRequest) based on the format of the URI. But your URI is just a bare address:

"100.100.0.35"

If you prepend a protocol, it should be able to determine what it needs from the URI:

"ftp://100.100.0.35"
David
  • 208,112
  • 36
  • 198
  • 279
  • Hi David I have tried this but given an error Illegal characters in path exception on this line `FileStream fs = File.OpenRead(source); ` – shruthi Jun 13 '14 at 10:18
  • @shruthi: And what is the value of `source` when that happens? You need to examine these runtime values and do at least *some* debugging. – David Jun 13 '14 at 10:43