c# upload a byte[] inside an FTP server

六月ゝ 毕业季﹏ 提交于 2020-01-15 01:21:23

问题


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:

  1. i see the client connecting to the server
  2. the file is created
  3. no data are transferred
  4. at some point the thread is terminated the connection is closed
  5. 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


回答1:


One issue that I see with the code is that you are writing the same bytes to the server on each iteration:

while (total_bytes > 0)
{
    strm.Write(messageContent, 0, buffLength); 
    total_bytes = total_bytes - buffLength;
}

You need to change the offset position by doing something like this:

while (total_bytes < messageContent.Length)
{
    strm.Write(messageContent, total_bytes , bufferLength);
    total_bytes += bufferLength;
}



回答2:


You are trying to write more data than you have. You code writes blocks of 2048 bytes at a time, and if the data is less, you will tell the write method to try to access bytes that are outside the array, which it of course won't.

All you should need to write the data is:

Stream strm = reqFTP.GetRequestStream();
strm.Write(messageContent, 0, messageContent.Length);
strm.Close();

If you need to write the data in chunks, you need to keep track of the offset in the array:

int buffLength = 2048;
int offset = 0;

Stream strm = reqFTP.GetRequestStream();

int total_bytes = (int)messageContent.Length;
while (total_bytes > 0) {

  int len = Math.Min(buffLength, total_bytes);
  strm.Write(messageContent, offset, len);
  total_bytes -= len;
  offset += len;
}

strm.Close();


来源:https://stackoverflow.com/questions/10479131/c-sharp-upload-a-byte-inside-an-ftp-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!