技术开发 频道

使用wse(Web Services Enhancements )把服务器端的文档传到客户端


【IT168技术文档】

  比如说,现在站点a有个附件想传给站点b,我们就能够用wse来传。
  在服务器端的webservice文档service1.asmx中写入webmethod:
  这个是取附件的方法:
[webmethod] public void getattachment() { // 获取 soapcontext 作为响应消息 soapcontext mycontext = httpsoapcontext.responsecontext; // 字符串,表示附件的文档名和路径。 string filepath = @"c:\my documents\1.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; byte[] attdata=new byte[length]; //把附件的流写入byte中 str.read(attdata,0,length); //创建新文档 fileinfo fi=new fileinfo(@"c:\"+sw.responsesoapcontext.attachments[0].id); filestream fs=fi.create(); //把byte写入新文档中 fs.write(attdata,0,length); fs.flush(); fs.close(); }
  注意:在webservice站点和客户端website站点的web.config中system.web节下加:
<webservices> <soapextensiontypes> <add type="microsoft.web.services.webservicesextension, microsoft.web.services, version=1.0.0.0, culture=neutral, publickeytoken=31bf3856ad364e35" priority="1" group="0" /> </soapextensiontypes> </webservices>
0
相关文章