Click here to Skip to main content
15,942,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
have written below code to copy file from local system to ftp server , similarly i need to select multiple files and upload them in server . While i upload the files it should be converted as encrypted files, any suggestions, i have written code for encryption also.

What I have tried:

public class WebRequestGetExample  
{  
public static void Main ()  
{  
// Get the object used to communicate with the server.  
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://192.1....../ftp.txt"); request.Method =WebRequestMethods.Ftp.UploadFile;  
// This example assumes the FTP site uses anonymous logon.  
request.Credentials = new NetworkCredential("username", "password");  
// Copy the contents of the file to the request stream.  
byte[] fileContents;  
using (StreamReader sourceStream = new StreamReader("E:\\Anusha\\ftp.txt"))  
{  
fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());  
}  
request.ContentLength = fileContents.Length;  
using (Stream requestStream = request.GetRequestStream())  
{  
requestStream.Write(fileContents, 0, fileContents.Length);  
}  
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())  
{  
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);  
}  
}  
}
Posted
Updated 8-May-18 17:28pm
Comments
Richard MacCutchan 8-May-18 3:25am    
To upload multiple files you need to do it in a loop. And you can encrypt them before sending, or on receipt at the server.
Member 13777104 8-May-18 4:50am    
Hi Richard,
I want to encrypt the file while uploading in to ftp server, like when we click on a file we need to encrypt as we all as upload in ftpserver and when we click on download in server it should decrypt. any idea on this

1 solution

WebClient offers an easy way to upload and download using FTP, see: c# - Upload file and download file from FTP - Stack Overflow[^]
You can encrypt using the System.Security.Cryptography namespace in .NET.
Here is a good article about encryption on CodeProject: Swanky Encryption/Decryption in C#[^]
 
Share this answer
 
v2
Comments
Member 13777104 8-May-18 4:46am    
Hi, I have written below code to upload multiple files but the certain folder was not updated in ftp server,please helpwhere i went wrong.
public class WebRequestGetExample
{
public static void Main ()
{
try
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://192...../E:\\folder1");
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("username", "pwd");
// Copy the contents of the file to the request stream.
IEnumerable<string> files = Directory.EnumerateFiles(@"E:\\folder1");
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential("username", "password");
foreach (string file in files)
{
client.UploadFile("ftp://192..../E:\\folder1" + Path.GetFileName(file), file);
}
}

using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
}
}
catch(Exception e)
{

}
}
RickZeeland 8-May-18 14:20pm    
Maybe you need to set ftpRequest.UseBinary to true, see example here: https://www.codeproject.com/Articles/667201/FtpWebRequest
For large files you may need to set the ftpRequest.Timeout value.
Member 13818142 8-May-18 23:30pm    
Hi i am getting error as The name 'ftpRequest' does not exist in the current context what should i do
RickZeeland 9-May-18 2:26am    
In your code it's named 'request' ...
Member 13818142 9-May-18 2:36am    
sorry sir i have complied above code but unable to display files in ftp server not sure where to change the code searched examples but i am not sure where to change please suggest, my loop is getting terminated after clientuploadfile(...)statment

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