做一项目,要求使用web service实现文件的上传,我一点头绪都没有,请教高手给予指点。

解决方案 »

  1.   

    http://www.codeproject.com/soap/ImageUploadWS.asp
      

  2.   

    其实二楼的已经给你提供了一个用C#的解决方案,其基本原理如下:
    1、在WebServices中声明一个方法,并以字节数组作为参数;
    putimage(bytep[] ImgIn)
    2、在页面中把你要上传的图片转换为字节数组。
    3、调用webservices的pubimage方法,并传递上面的字节数组。这是C#的解决方案。如果用delphi来做,原理是类似的
      

  3.   

    参考李维的<Delphi SOAP/WEB SERVICE 开发>那个很详细。。
      

  4.   

    我刚才翻了一下Delphi的帮助,其实Delphi提供了一个更简单的方式用来实现Server和Client之间文件流的传递。那就是使用TSOAPAttachment,这是一个Remotable Object。封装到Soap协议中时成为单独的编码节点(multipart form)进行传输,很有点类似于电子邮件中的附件。那就是说,定义一个Webservice方法,以TSOAPAttachment作为参数类型。客户端同样生成一个TSOAPAttachment的对象,传递过去。
      

  5.   

    用ms soap toolkit做,看他的提供帮助,收索DIME关键字,会有关于接收附件的例子
      

  6.   

    把文件进行Base64编码后,就象发字符串一样发给服务再解码就行
      

  7.   

    1 web service 是基于xml的一个东东。
    2 web service 就是你提供一个函数,供远程计算机调用,
      你不必关心传输调用过程,因为都封装好了,你只需要
      实现这个函数,并把结果放到result里,远程计算机就回
      得到这个结果。
    不能贴完所有的东西,贴几个主要的在服务端impl实现单元里:implementation
    var
    fm:TmemoryStream;
    ......
    ......
    initialization
      { Invokable classes must be registered }
      InvRegistry.RegisterInvokableClass(TmyTestWebservice);
      fm:=TmemoryStream.Create;
    finalization
      fm.Free;服务端具体实现函数:function TmyTestWebservice.UpdateFile(fname, subText: widestring;
      pos: integer;fsize:integer): integer;
    var
     buf2:pchar;
     buf3:array of byte; fn:widestring;
     s:string;
    begin
     result:=-1;//表示尚未成功
     try
      s:=string(subtext); //重新单字节
      if (s='ED') and (pos=0) and (fsize=0) then//文件结束协议。
         begin
          fm.SaveToFile(fn);
          fm.Clear;
          exit;
         end;
      fn:=fname;
      delete(fn,1,3);
      fn:='D:\'+fn;
      getmem(buf2,fsize);
      HexToBin(pchar(s),buf2,fsize); //把二进制的字符串转化为byte数组
      setlength(buf3,fsize);
      ZeroMemory(buf3,fsize);
      copymemory(@buf3[0],buf2,fsize);//转为数据流
      fm.Seek(pos,0);
      fm.WriteBuffer(buf3[0],fsize);
      result:=0;
     finally
      freemem(buf2);
     end;
    end;
    //客户端上传函数的使用
    procedure TForm1.Button1Click(Sender: TObject);
    const
    Psize=1024;
    var
    i:integer;
    Is1:ImyTestWebservice;
    fs:Tfilestream;
    buf:array of byte;
    s:widestring;
    pos,CountSize,fsize:integer;
    begin
    try
      Is1:=GetImyTestWebservice;
      fs:=Tfilestream.Create(self.Edit1.Text,fmShareDenyNone);
      countsize:=fs.Size;
      Pos:=0;
      fsize:=psize;  while true do
       begin
        if countsize<=fsize then
          fsize:=countsize;
          setlength(buf,fsize);
        zeromemory(@buf[0],fsize);
        fs.Seek(pos,0);
        fs.Readbuffer(buf[0],length(buf));
        s:='';
        for i:=0 to fsize-1 do
         begin
          s:=s+inttohex(buf[i],2);
         end;
         //把这个二进制数组变成字符串。
         //这就是.net的序列化,因为web service基于xml
         //上传
         if length(s)=0 then exit;
         is1.UpdateFile(self.Edit1.Text,s,pos,fsize);//上传函数。
         pos:=pos+fsize;
         countsize:=countsize-fsize;
         if countsize=0 then
          begin
          is1.UpdateFile(self.Edit1.Text,'ED',0,0);//上传函数结束。
          exit;
          end;
      end;
     finally
      fs.Free;
     end;
    end;
    以上为CGI的webservice上传二进制文件,win2003+ D7调试通过。
      

  8.   

    HexToBin(pchar(s),buf2,fsize); //把二进制的字符串转化为byte数组for i:=0 to fsize-1 do
         begin
          s:=s+inttohex(buf[i],2);
         end;这个费时间,用TByteDynArray 类型,就可以省略这1步,因为Delphi会
    自行转化。或者用base64转化也可。