2009-09-10 89 views
0

我是新來的.net遠程處理,我做了幾個示例應用程序上.net remoting.i可以很容易地 從服務器通過遠程對象獲取文件,但我不知道如何發送文件到服務器端,如果有可能通過接口意味着如何設計它。給我一些建議和鏈接,這對我來說是正確的方向遠程文件傳輸

回答

1

要發送一個文件,您可以重複調用服務器上的一個方法,以塊爲單位給它一個文件塊。像這樣:

static int CHUNK_SIZE = 4096; 

// open the file 
FileStream stream = File.OpenRead("path\to\file"); 

// send by chunks 
byte[] data = new byte[CHUNK_SIZE]; 
int numBytesRead = CHUNK_SIZE; 
while ((numBytesRead = stream.Read(data, 0, CHUNK_SIZE)) > 0) 
{ 
    // resize the array if we read less than requested 
    if (numBytesRead < CHUNK_SIZE) 
     Array.Resize(data, numBytesRead); 

    // pass the chunk to the server 
    server.GiveNextChunk(data); 
    // re-init the array so it's back to the original size and cleared out. 
    data = new byte[CHUNK_SIZE]; 
} 

// an example of how to let the server know the file is done so it can close 
// the receiving stream on its end. 
server.GiveNextChunk(null); 

// close our stream too 
stream.Close(); 
1

你必須實現這種行爲。客戶端讀取文件併發送字節。服務器接收字節並寫入文件。還有更多,但這是你需要做的基礎。