English 中文(简体)
c 专用自动交换机服务器内的自动取款机
原标题:c# upload a byte[] inside an FTP server
  • 时间:2012-05-07 08:44:42
  •  标签:
  • c#
  • ftp
  • arrays

我需要上载一些DATA的服务器和FTP服务器。

跟踪盘点后,如何在燃料循环基金内部卸载燃料和燃料循环基金所有工作。

现在我正在努力改进我的上载。

相反,收集DATA,将其写给FLE公司,然后将档案上载到FTP内,以便收集DATA,在不创建当地档案的情况下将其上载。

为此:

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();

现在情况如下:

  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.

意欲转让的DATA是一件战略文件,这就是为什么要发出信息Content = 编码。

什么是错了?

此外,如果与ASCII号编码相联。 遥远服务器上的GetBytes将拥有一个短信文档或一个与一些Bytes的档案?

感谢您提出任何建议

最佳回答

我在守则中看到的一个问题是,你在每台电机上写到same:

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

你们需要改变这种被抵消的立场,做的是:

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

你试图撰写比你更多的数据。 你的代码在某一时间写上了2048年的盒子,如果数据较少,你将使用<条码>write的方法,试图让位于阵列之外、但当然会赢得的箱子进入。

你们都需要撰写数据:

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

如果你需要用草坪书写数据,你需要跟踪阵列中被抵消的情况:

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();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签