我需要在FTP服务器上上传一些数据。
按照stackoverflow的帖子上传文件,一切正常。
现在,我正在尝试改进我的上传。
我想收集数据并上传它们,而不是先将它们写到一个文件中,然后将该文件上传到FTP。
为了实现这一点,我做如下操作:
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();
现在发生的情况如下:
- 我看到客户端正在连接服务器
- 文件已创建
- 没有传输数据
- 在某个点上线程被终止,连接被关闭
- 如果我检查上传的文件是空的。
我错在哪里了?
此外:如果我使用ASCII.GetBytes编码日期,在远程服务器上将会得到一个文本文件还是一些字节的文件?
谢谢任何建议。