使用wse(Web Services Enhancements )把伺服器端的檔案傳到用戶端
來源:互聯網
上載者:User
比如說,現在網站A有個附件想傳給網站B,我們就可以用WSE來傳。
在伺服器端的webservice檔案service1.asmx中寫入webmethod:
這個是取附件的方法:
[WebMethod]
public void GetAttachment()
{
// 擷取 SoapContext 作為響應訊息
SoapContext myContext = HttpSoapContext.ResponseContext;
// 字串,表示附件的檔案名稱和路徑。
string filePath = @"C:My Documents1.txt";
// 使用檔案名稱來建立新的 DIME 附件,
// 並通過 MIME 媒體類型
// 來指定附件編碼。
DimeAttachment dimeImage = new DimeAttachment(
"text/xml", TypeFormatEnum.MediaType,
filePath);
// 指定給 DIME 記錄的 ID 屬性。
dimeImage.Id = "1.txt";
// 將新的 DimeAttachment 對象添加到 SoapContext 對象中。
myContext.Attachments.Add(dimeImage);
}
在用戶端的web網站下載附件,並寫入到新的檔案中:
private void DownAttachment()
{
//Service1WSE : Microsoft.Web.Services.WebServicesClientProtocol 這裡的Service1繼承WSE 中的類
Service1WSE sw=new Service1WSE();
//從webservice中擷取附件
sw.GetAttachment();
//得到附件的流
Stream str=sw.ResponseSoapContext.Attachments[0].Stream;
int length=(int)str.Length;