I have to upload a local file in a sftp folder. Using SSH.NET I wrote this code
using (var client = new ScpClient(host, port, user, password))
{
client.Connect();
FileInfo file = new FileInfo(fileFullName);
string fileName = Path.GetFileName(fileFullName);
client.Upload(file, uploadFolder + fileName);
}
I can connect and also verified it writing if (client.IsConnected)
, but I receive the error Renci.SshNet.Common.ScpException: Receive file existence status cannot be determined
.
The folder is correct (I copied it from FileZilla and uploadFolder+fileName
return something like ftpFolder/fileName.txt
) I have authorization (using same credentials, I can put a file in the sftp folder from FileZilla) the local file path is correct (I verified it with debug).
The error is about the client.Upload()
function.
It seems that it check if the file exists, but I don't want to change an existing file, I want to upload a new one.
Is it the wrong method?
I also tryied this code
using (var client = new SftpClient(host, port, user, password))
{
client.Connect();
using (var fs = File.OpenRead(fileFullName))
{
client.UploadFile(fs, uploadFolder, true);
}
}
but I receive System.Exception: 'General failure '
(always in client.Upload
Can someone help me?
I've solved it. I had to use SftpClient, not ScpClient that's not working for me. To solve the General failure, it simply needed to insert the buffer size! Working code:
string fileName = Path.GetFileName(fileFullName);
using (var client = new SftpClient(host, port, user, password))
{
client.Connect();
using (var fs = File.OpenRead(fileFullName))
{
client.BufferSize = 4 * 1024;
client.UploadFile(fs, uploadFolder + fileName);
}
}