File Uploaded With no data

Mar 26, 2012 at 5:32 PM

Hi i am trying to encrypt my file and upload it to the server. below is the code i am using and i am getting an empty file on my server.

 

 using (MemoryStream temp = new MemoryStream())
            {
                using (var inputStream =
                  new FileStream(localfilename, FileMode.Open, FileAccess.Read))
                {
                    var connectionInfo = new ConnectionInfo(host, 22, username,
                           new PasswordAuthenticationMethod(username, password),
                           new PrivateKeyAuthenticationMethod(username, new PrivateKeyFile(File.OpenRead(privatekeyfile), passphrase)));

                    // Create an encryption stream.
                    using (var cryptoStream =
                       new CryptoStream(temp,
                          crypto.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        // Copy the input file stream to the encryption stream.
                        inputStream.CopyTo(cryptoStream);

                        
                        Renci.SshNet.SftpClient sftpclient = new Renci.SshNet.SftpClient(connectionInfo);
                        string remotefilename = "/Test/" + System.IO.Path.GetFileName(localfilename);
                        try
                        {

                            sftpclient.Connect();
                            if (sftpclient.IsConnected)
                            {

                                sftpclient.UploadFile(temp, remotefilename, true);
                            }

                        }
                        catch (Exception ex)
                        {
                           

                            throw (ex);
                        }
                        finally
                        {

                            sftpclient.Disconnect();
                           
                        }
                    }
                }
            }
Coordinator
Apr 13, 2012 at 1:22 PM

Hi,

 

Can you check temp size to see if its not zero?

 

Thanks,

Oleg

Apr 13, 2012 at 1:41 PM

Hi Olegkap,

 

thanks for the reply

 

i resolved the issue and its because the file is already open by another process and not been released.