File biner unggahan C# Ftp tidak dapat diunggah

Mencoba mencari file dan mengunggahnya ke server ftp, saya rasa semuanya sudah benar tetapi tidak mengunggah apa pun

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

       }

Coba juga memasukkan lokasi file sebagai string dengan @"C... Saya tidak menerima kesalahan, dan tidak ada file yang muncul setelah diunggah


person user2184248    schedule 27.12.2013    source sumber
comment
Sudahkah Anda mencoba menjalankan kode di debugger dan memeriksa konten variabel saat sedang berjalan? Kami tidak dapat melihatnya dari sini, dan tidak tahu apakah file tersebut ada di tempat yang Anda cari atau tidak. Anda harus men-debugnya sendiri untuk melihat apa yang gagal.   -  person Ken White    schedule 27.12.2013
comment
Ya, file tersebut ada di lokasi itu, bahkan mencobanya dengan file teks dengan nama tertentu, tetapi tidak ada yang berhasil   -  person user2184248    schedule 27.12.2013
comment
debugger tidak membuat kesalahan   -  person user2184248    schedule 27.12.2013
comment
Saya tidak mengatakan apa pun tentang kesalahan yang dilakukan. Silakan baca apa yang saya tulis. Kita tidak dapat menelusuri kode untuk melihat apa yang salah, karena kita tidak dapat melihat apa yang dilakukannya saat runtime. Hanya Anda yang dapat melakukannya, karena hanya Anda yang memiliki akses untuk melakukannya.   -  person Ken White    schedule 27.12.2013


Jawaban (1)


Apakah Anda memeriksa izin pengguna di server.. dapatkah pengguna menulis ke direktori?

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