-1

This is a working code that upload text file to my ftp root. Tested and working. But now i want to create a sub directory on the root directory and then after creating the directory to upload the file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;

namespace mws
{
    class FtpFileUpload
    {
        static string ftpurl = "ftp://ftp.newsxpressmedia.com/";
        static string filename = @"c:\temp\FtpTestFile.txt";
        static string ftpusername = "Username";
        static string ftppassword = "Password";
        static string ftpdirectory = "subtestdir";

        public static void test()
        {
            try
            {
                FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(
                ftpurl + "/" + Path.GetFileName(filename));
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = new NetworkCredential(ftpusername, ftppassword);
                StreamReader sourceStream = new StreamReader(@"c:\temp\test1.txt");
                byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
                sourceStream.Close();
                request.ContentLength = fileContents.Length;
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(fileContents, 0, fileContents.Length);
                requestStream.Close();
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

                response.Close();
            }
            catch (Exception err)
            {
                string t = err.ToString();
            }
        }
    }
}

I tried to change the first line and added also a line to create a directory:

FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(
                ftpurl + "/" + ftpdirectory + "/" + Path.GetFileName(filename));
                request.Method = WebRequestMethods.Ftp.MakeDirectory;

But then i'm getting exception error:

The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

But before the changes it's working fine no problems. How can i create a directory on my server first and then upload the file to the created directory ?

Daniel Lip
  • 3,867
  • 7
  • 58
  • 120
  • possible duplicate of [How do I create a directory on ftp server using C#?](http://stackoverflow.com/questions/860638/how-do-i-create-a-directory-on-ftp-server-using-c) – Ken White Nov 09 '14 at 21:22

2 Answers2

0

Have you checked this answer : How do I create a directory on ftp server using C#?
It looks like the path you're giving to the MakeDirectory request is actually a file name, not a directory name.

Community
  • 1
  • 1
Matt Ko
  • 969
  • 7
  • 14
  • You have to make two requests. You cannot do this in one request, as suggested by the answer to the question that I referenced. So you first create the directory, then you upload the file. – Matt Ko Nov 09 '14 at 22:25
  • In the end i used this codeproject code: http://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class and i'm using my own file upload method and also added some changes and options and it's working perfect now. – Daniel Lip Nov 09 '14 at 22:50
-1
public static void CreateDirectoryandSaveImage(string username, string password)
    {
       string ftphost = "ftp.placemyorder.com";
        string filePath = @"D:\ddlState.png";
        //You could not use "ftp://" + ftphost + "/FTPUPLOAD/WPF/WPF.txt" to create the folder,this will result the 550 error.
        string ftpfullpath = "ftp://" + ftphost + "/TestFolder";
    //    //Create the folder, Please notice if the WPF folder already exist, it will result 550 error.
        FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
        ftp.Credentials = new NetworkCredential("ftp.placemyorder.com|placemyorder", "Cabbage123");
        ftp.UsePassive = true;
        ftp.UseBinary = true;
        ftp.KeepAlive = false;
        ftp.Method = WebRequestMethods.Ftp.MakeDirectory;
       FtpWebResponse CreateForderResponse = (FtpWebResponse)ftp.GetResponse();
        if (CreateForderResponse.StatusCode == FtpStatusCode.PathnameCreated)
        {
           //If folder created, upload file
            var fileName = Path.GetFileName(filePath);
            string ftpUploadFullPath = "ftp://" + ftphost + "/TestFolder/" + fileName;
            FtpWebRequest ftpUpLoadFile = (FtpWebRequest)FtpWebRequest.Create(ftpUploadFullPath);
            ftpUpLoadFile.KeepAlive = true;
           ftpUpLoadFile.UseBinary = true;
          ftpUpLoadFile.Method = WebRequestMethods.Ftp.UploadFile;
            ftpUpLoadFile.Credentials = new NetworkCredential(username, password);
            ftpUpLoadFile.UsePassive = true;
            ftpUpLoadFile.UseBinary = true;
            ftpUpLoadFile.KeepAlive = false;
           FileStream fs = File.OpenRead(filePath);
            byte[] buffer = new byte[fs.Length];
           fs.Read(buffer, 0, buffer.Length);
           fs.Close();
           Stream ftpstream = ftpUpLoadFile.GetRequestStream();
          ftpstream.Write(buffer, 0, buffer.Length);
           ftpstream.Close();
      }

    }
Exhausted
  • 1,867
  • 2
  • 23
  • 33