ไฟล์ไบนารีที่อัปโหลด C # Ftp จะไม่อัปโหลด

กำลังพยายามค้นหาไฟล์และอัปโหลดไปยังเซิร์ฟเวอร์ ftp ฉันคิดว่าทุกอย่างถูกต้องแล้ว แต่ไม่ได้อัปโหลดอะไรเลย

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();

       }

ลองใส่ตำแหน่งไฟล์เป็นสตริงด้วย @"C... ฉันไม่ได้รับข้อผิดพลาด และไม่มีไฟล์ปรากฏขึ้นหลังจากการอัปโหลด


person user2184248    schedule 27.12.2013    source แหล่งที่มา
comment
คุณได้ลองรันโค้ดในตัวดีบักเกอร์และตรวจสอบเนื้อหาของตัวแปรในขณะที่มันกำลังทำงานอยู่หรือไม่? เรามองไม่เห็นจากที่นี่ และไม่รู้ว่าไฟล์นั้นอยู่ที่ใดที่คุณกำลังมองหาอยู่หรือไม่ คุณจะต้องแก้ไขข้อบกพร่องด้วยตัวเองเพื่อดูว่ามีอะไรผิดพลาดบ้าง   -  person Ken White    schedule 27.12.2013
comment
ใช่ ไฟล์อยู่ในตำแหน่งนั้น แม้จะลองด้วยไฟล์ข้อความที่มีชื่อเฉพาะ แต่ก็ไม่มีอะไรทำงาน   -  person user2184248    schedule 27.12.2013
comment
ดีบักเกอร์ไม่มีข้อผิดพลาด   -  person user2184248    schedule 27.12.2013
comment
ฉันไม่ได้พูดอะไรเกี่ยวกับการขว้างข้อผิดพลาด โปรดอ่านสิ่งที่ฉันเขียน เราไม่สามารถก้าวผ่านโค้ดเพื่อดูว่าเกิดอะไรขึ้น เนื่องจากเราไม่สามารถเห็นได้ว่ามีอะไรเกิดขึ้นในขณะรันไทม์ มีเพียงคุณเท่านั้นที่สามารถทำได้ เพราะมีเพียงคุณเท่านั้นที่สามารถเข้าถึงได้   -  person Ken White    schedule 27.12.2013


คำตอบ (1)


คุณได้ตรวจสอบสิทธิ์ผู้ใช้บนเซิร์ฟเวอร์แล้ว.. ผู้ใช้สามารถเขียนไปยังไดเร็กทอรีได้หรือไม่?

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**********");
                }
            }
person charith rasanga    schedule 27.12.2013