I need to upload some DATA inside and FTP server.
Following stackoverflow posts on how to upload a FILE inside and FTP everything works.
Now i am trying to improve my upload.
Instead collecting the DATA, writing them to a FILE and then upload the file inside the FTP i want to collect the DATA and upload them without creating a local file.
To achieve this i do the following:
string uri = "ftp://" + ftpServerIp + "/" + fileToUpload.Name;
System.Net.FtpWebRequest reqFTP;
// Create FtpWebRequest object from the Uri provided
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIp + "/" + fileToUpload.Name));
// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
// By default KeepAlive is true, where the control connection is not closed after a command is executed.
reqFTP.KeepAlive = false;
// Specify the command to be executed.
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// Specify the data transfer type.
reqFTP.UseBinary = true;
byte[] messageContent = Encoding.ASCII.GetBytes(message);
// Notify the server about the size of the uploaded file
reqFTP.ContentLength = messageContent.Length;
int buffLength = 2048;
// Stream to which the file to be upload is written
Stream strm = reqFTP.GetRequestStream();
// Write Content from the file stream to the FTP Upload Stream
int total_bytes = (int)messageContent.Length;
while (total_bytes > 0)
{
strm.Write(messageContent, 0, buffLength);
total_bytes = total_bytes - buffLength;
}
strm.Close();
Now what happen is the following:
- i see the client connecting to the server
- the file is created
- no data are transferred
- at some point the thread is terminated the connection is closed
- if i inspect the uploaded file is empty.
the DATA i want to to transfer is a STRING TYPE, that is why i do byte[] messageContent = Encoding.ASCII.GetBytes(message);
what am i doing wrong?
moreover: if i encode date with ASCII.GetBytes, on the remote server will i have a TEXT file or a file with some Bytes?
thank you for any suggestion