简体   繁体   中英

C# overwrite file with empty file when trying to upload (ftp)

I am trying to upload text file over ftp, and when I run program, it empty my file and then upload it. I don't know why... May it overwrite or something else... Here is my code:

This is in Main class

        /* Create Object Instance */
        ftp ftpClient = new ftp(@"ftp://sportcaffe.me", "sport***", "*****");

        /* Upload a File */
        ftpClient.upload("public_html/test.txt", @"C:\Users\Lazar\Desktop\test.txt");

And here is ftp class function code:

/* Upload File */
    public void upload(string remoteFile, string localFile)
    {
        try
        {
            /* Create an FTP Request */
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
            /* Log in to the FTP Server with the User Name and Password Provided */
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            /* When in doubt, use these options */
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            /* Specify the Type of FTP Request */
            ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
            /* Establish Return Communication with the FTP Server */
            ftpStream = ftpRequest.GetRequestStream();
            /* Open a File Stream to Read the File for Upload */
            FileStream localFileStream = new FileStream(localFile, FileMode.Create);
            /* Buffer for the Downloaded Data */
            byte[] byteBuffer = new byte[bufferSize];
            int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
            /* Upload the File by Sending the Buffered Data Until the Transfer is Complete */
            try
            {
                while (bytesSent != 0)
                {
                    ftpStream.Write(byteBuffer, 0, bytesSent);
                    bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
            /* Resource Cleanup */
            localFileStream.Close();
            ftpStream.Close();
            ftpRequest = null;
        }
        catch (Exception ex) { MessageBox.Show(ex.ToString()); }
        return;
    }

There is no errors and warnings...

first I write something in my file and save, then when I run program my file is empty and that empy file has been uploaded...

i used this code http://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class

FileStream localFileStream = new FileStream(localFile, FileMode.Create);

I hope I don't need to spell this out too clearly but the above line is being told to create a file. To quote the docs: "Specifies that the operating system should create a new file. If the file already exists, it will be overwritten."

http://msdn.microsoft.com/en-us/library/system.io.filemode(v=vs.110).aspx has a list of the options you can use instead here. FileMode.Open should be good for your needs (it will open the file and if the file doesn't exist throw an exception).

Solved

FileStream localFileStream = new FileStream(localFile, FileMode.Create); replace FileMode.Create by FileMode.Open

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM