2012-05-07 11 views
3

内部にFTPサーバーとDATAをアップロードする必要があります。c#FTPサーバー内にバイト[]をアップロード

ファイルとFTPをアップロードする方法については、stackoverflowの投稿に続いてすべてが動作します。

アップロードを改善しようとしています。

DATAを収集し、FILEに書き込んだ後、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は、ファイルが作成されたサーバー
  2. に接続しているクライアントを参照してください
  3. データが転送されない
  4. ある時点でスレッドが終了し、接続が閉じられる
  5. アップロードしたファイルが空であるかどうかを調べると

私が転送したいDATAはSTRING TYPEです。そのため、私はbyte []を行います。messageContent = Encoding.ASCII.GetBytes(message);

何が間違っていますか?

さらに:ASCII.GetBytesで日付をエンコードすると、リモートサーバー上にTEXTファイルまたはいくつかのバイトを持つファイルがありますか?

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

、変更する必要があります:

は、私がコードで参照

+0

STRM:あなたはチャンクでデータを書き込む必要がある場合は

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

は、アレイ内のオフセットを追跡する必要があります.Write(messageContent、0、messageContent.lenght);解決策です。ちょうどこれのように私は一度にすべてのファイルを書き込むだろうと私は巨大なファイルで何が起こるか知りません。 – NoobTom

答えて

4

一つの問題は、あなたが各反復上のサーバに同じバイトを書いているということである任意の提案をありがとうございました

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

あなたはあなたよりも多くのデータを書き込もうとしています。コードは一度に2048バイトのブロックを書き込み、データがそれより少ない場合は、writeメソッドに配列外のバイトにアクセスしようとしますが、これは当然行われません。

すべてあなたがデータを書き込む必要があるのは、次のとおりです。

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(); 
関連する問題