Click here to Skip to main content
16,007,843 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Guys,

Hey im trying to copy all the files from a directory on my machine to another directory on the FTP server(The folder to which i want to copy my files doesnot exist on the ftp server)...Im getting an exception ----> Cannot send a content-body with this verb-type.Here---> Stream reqStream = request.GetRequestStream();

Here is my code please help me out fellas.
C#
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftp://192.168.1.119/images");
 request.Credentials = new NetworkCredential("Rahul Middha","jkd");
request.Method = WebRequestMethods.Ftp.MakeDirectory;
            request.UsePassive = true;
            request.UseBinary = true;
            request.KeepAlive = false;


            DirectoryInfo di = new DirectoryInfo("C:\\Datacap\\Demo\\images");
            FileInfo[] files=null;
            if (di.Exists)
            {
                files = di.GetFiles("*.TIF");
            }
            FileStream fs = null;
            int buffLength = 2048;
            byte[] buff = new byte[buffLength];
            foreach (FileInfo fi in files)
                {

                    fs = fi.OpenRead();
                    fs.Read(buff, 0, buff.Length);
                }

            fs.Close();

            Stream reqStream = request.GetRequestStream();
            reqStream.Write(buff, 0, buff.Length);
            reqStream.Close();


[Edit(zorgoz): code block added, shouting removed]
Posted
Updated 28-Aug-12 21:36pm
v2
Comments
Gautam Boddeda 29-Aug-12 4:43am    
Karnan Sekar: Bro im trying to write the stream of bytes to the ftp server...i have like 100's of files on my machine and want to upload them to the ftp server...as well as creating a directory...please help me out with this

1 solution

private static void UploadViaFtp(string pWebsitePath, string pFileName)
{
string sFtpaddress = ConfigurationSettings.AppSettings["ftpaddress"].ToString();
string sFtpusrname = ConfigurationSettings.AppSettings["ftpusrname"].ToString();
string sFtppwd = ConfigurationSettings.AppSettings["ftppwd"].ToString();



FtpWebRequest request = (FtpWebRequest)WebRequest.Create(sFtpaddress + "/" + pFileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(sFtpusrname, sFtppwd);


StreamReader sourceStream = new StreamReader(pWebsitePath + pFileName);
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();

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900