WCF流與檔案傳輸 資料契約和訊息契約

2021-08-01 11:38:16 字數 2194 閱讀 9308

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;

using system.servicemodel;

using system.servicemodel.description;

using system.runtime.serialization;

using system.reflection;

using system.io;

namespace wcfdemo1

); try

catch (exception ex)

console.readkey();

}/* 備註

* 利用資料契約已經能夠很好地完成資料的傳輸了,而資料契約只能控制訊息體,

* 有時候我們想在資料傳遞過程中新增一些額外資訊,而不希望新增額外的契約字段,

* 那麼我們就得改訊息報頭,也就是說該使用訊息契約了

* 訊息契約第乙個典型應用就是在執行檔案傳輸時,檔案的二進位制資訊放到body裡,

* 而一些復加的檔案資訊則放在head裡*/}

}[servicecontract(namespace = "mynamespace")]

public inte***ce iservice

public class myservice : iservice

try接收到客戶端傳送的流,已儲存到。", datetime.now.tolongtimestring(), tmsg.file_name);}}

catch (exception ex)

return rmsg;

} }

[messagecontract]

public class transferfilemessage

[messagecontract]

public class resultmessage

}

using system;

using system.collections.generic;

using system.componentmodel;

using system.data;

using system.drawing;

using system.io;

using system.linq;

using system.text;

using system.threading.tasks;

using system.windows.forms;

namespace client

private void btnselectfile_click(object sender, eventargs e)

}private async void btntransfer_click(object sender, eventargs e)

filestream fs = new filestream(this.lbselectedfilename.text, filemode.open, fileaccess.read);

ws.serviceclient cl = new ws.serviceclient();

this.btntransfer.enabled = false;

var response = await cl.uploadfileasync(path.getfilename(this.lbselectedfilename.text), fs);

this.btntransfer.enabled = true;

if (response.issuccessed == true)

this.lbmessage.text = "上傳完成。";

else

this.lbmessage.text = "錯誤資訊:" + response.errormessage; }}

WCF檔案傳輸

本文和大家一起分享利用wcf實現檔案的傳輸。程式執行效果 接收檔案端 傳送檔案端 連線wcf服務,選擇要傳輸的檔案 檔案傳輸成功 我們會在儲存檔案的預設路徑 c documents and settings administrator 桌面,下看到傳輸的檔案 code using system us...

IO流檔案傳輸

1.reader與writer readline 使用string io流檔案雙向傳輸 複製檔案 儲存到string中 file fin new file resources my.txt file fou new file resources new.txt bufferedreader ir n...

C 之輸入輸出流和檔案傳輸流

ios是抽象基類,由它派生出istream類和ostream類,iostream類支援輸入輸出操作,iostream類是從istream類和ostream類通過多重繼承而派生的類 類ifstream繼承了類istream,類ofstream繼承了類ostream,類fstream繼承了 類iostr...