0

Trying to Find a file and upload it to ftp server, I think i have everything correct but it doesnt upload anything

string filepath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        DirectoryInfo d = new DirectoryInfo(filepath);

        List<String> allDatfiles = Directory
               .GetFiles(filepath, "data.dat", SearchOption.AllDirectories).ToList();

        foreach (string file in allDatfiles)
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.test.com/Holder");
                request.Method = WebRequestMethods.Ftp.UploadFile;

                request.Credentials = new NetworkCredential("User", "Pass");
                request.UseBinary = true;
                request.UsePassive = true;
                byte[] data = File.ReadAllBytes(file); // Think the problem is with file
                request.ContentLength = data.Length;
                Stream stream = request.GetRequestStream();
                stream.Write(data, 0, data.Length);
                stream.Close();

       }

Also tried putting in the file location as a string with @"C... I receive no errors, and no file shows up after the upload

Adarsh Shah
  • 6,755
  • 2
  • 25
  • 39
user2184248
  • 27
  • 1
  • 9
  • Have you tried running the code in the debugger and examining the variable contents while it's running? We can't see them from here, and have no idea whether the file is where you're looking for it or not. You're going to have to debug this yourself to see what's failing. – Ken White Dec 27 '13 at 03:19
  • Yes the file is in that location, even tried it with a text file with a specific name, but nothing works – user2184248 Dec 27 '13 at 03:27
  • the debugger throws no errors – user2184248 Dec 27 '13 at 03:28
  • I didn't say anything about "throwing errors". Please read what I wrote. We cannot step through the code to see what's going wrong, because we can't see what it's doing at runtime. Only you can do that, because only you have access to it to do so. – Ken White Dec 27 '13 at 03:29

1 Answers1

0

Did you check the user permission on server.. can user write to directory?

if you use linux server this will help you to fix file path issue.

     private static void UploadFile(string dir, Uri target, string fileName, string username, string password, string finilizingDir, string startupPath, string logFileDirectoryName)
            {
                try
                {
                    // Get the object used to communicate with the server.
                    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
                    request.Proxy = null;
                    request.Method = WebRequestMethods.Ftp.UploadFile;

                    // logon.
                    request.Credentials = new NetworkCredential(username, password);

                    // Copy the contents of the file to the request stream.
                    StreamReader sourceStream = new StreamReader(dir + "\\" + fileName);
                    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();

                    if (response.StatusCode == FtpStatusCode.ClosingData)
                    {
                       Console.WriteLine(" --> Status Code is :" + response.StatusCode);
                    }

                     Console.WriteLine(" --> Upload File Complete With Status Description :" + response.StatusDescription);

                    response.Close();
                }
                catch (Exception ex)
                {
                     Console.WriteLine("*** Error Occurred while uploading file :" + fileName + " System Says :" + ex.Message + " **********END OF ERROR**********");
                }
            }
Community
  • 1
  • 1
charith rasanga
  • 124
  • 2
  • 3