Click here to Skip to main content
15,911,848 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The below is code for uploading files to the ftp server but it shows me file not found exception

FileInfo fileInf = new FileInfo(filename);


string uri = "ftp://" + ftpServerIP + path; //+ "/" + fileInf.Name;//


FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + path + "/" + filename));//"/" + awb +
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.Proxy = null;

reqFTP.ContentLength = fileInf.Length;

// The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;

// Opens a file stream (System.IO.FileStream) to read the file to be uploaded
FileStream fs = fileInf.OpenRead();

// using (FileStream stream = File.Open(@"D:\SSIMFROMFTPServer\", FileMode.Create));

// Stream to which the file to be upload is written
Stream strm = reqFTP.GetRequestStream();

//test
// FileStream writeStream = new FileStream(localDestnDir + "/" + strm, FileMode.Create);

// Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength);

// Till Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// Close the file stream and the Request Stream
strm.Close();
fs.Close();
Posted

1 solution

using WinSCP;

namespace Project
{
class Upload_Files
{
public void upload_file_sftp()
{
try
{

SessionOptions sessionOptions = new SessionOptions
{

Protocol = Protocol.Sftp,
HostName = "abc.com",
UserName = "abc",
Password = "1234",
PortNumber = 22,
SshHostKeyFingerprint = "ssh-rsa 1024 a7:20:03:28:19:3c:88:4c:05:3f:cf:c3:15:ad:57:20"

};


using (Session session = new Session())
{
;

session.Open(sessionOptions);


TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;




TransferOperationResult transferResult;
transferResult = session.PutFiles(@"P:\File_upload","/name_of_sftpfolder/", false, transferOptions);


transferResult.Check();



foreach (TransferEventArgs transfer in transferResult.Transfers)
{
MessageBox.Show("Upload of files " + transfer.FileName + " succeeded");


transferResult.Transfers.Count();


}


}



}
catch (Exception ex)
{
MessageBox.Show("Exception: " + ex.Message);
}


}
}
}

Note: For above code u need to add winscp dll and exe.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900