比如说,现在站点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>
