通過WebServices 訪問 服務器文件 (delphi6上編譯通過)

/////////////////////////////////////////////////////////////////////////
////////////////////Web Service 

unit Unit3;

interface
uses InvokeRegistry,Graphics,Classes,Unit2,Types;

type
  TQueryCapital 
= class(TInvokableClass, IQueryCapital)
  
public
    function GetPic:TByteDynArray; stdcall;
    function GetPicSize:LongInt; stdcall;
  end;

implementation

function ByteArrayFromStream( inStream : TMemoryStream ) : TByteDynArray;
var pTemp : pointer;
begin
  SetLength(Result, inStream.Size );
  pTemp :
= @Result[0];
  inStream.Position :
= 0;
  inStream.Read(pTemp
^, inStream.Size);
end;

//TByteDynArray 做返回值 是重點
function TQueryCapital.GetPic:TByteDynArray;
var
  tfs : TMemorystream;
  outByAr : TByteDynArray;
begin
  tfs:
=TMemorystream.Create;
  tfs.LoadFromFile(
'1.jpg');
  tfs.Position   :
=   0;

  Result :
= ByteArrayFromStream(tfs);
end;

function TQueryCapital.GetPicSize: LongInt;
var
  Pic : TPicture;
  tfs : TMemorystream;
  ss :
string;
begin
  ss :
='';
  tfs:
=TMemorystream.Create;
  tfs.LoadFromFile(
'1.jpg');
  tfs.Position   :
=   0;

  SetLength(ss,   tfs.Size);
  tfs.Read(ss[
1],   tfs.Size);
  Result :
= Length(ss);
end;

initialization
  InvRegistry.RegisterInvokableClass(TQueryCapital
{, ShakespeareFactory});
end.

/////////////////////////////////////////////////////////////////////////////
//////////Client

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,iquerycapital1,Types, ExtCtrls, StdCtrls, Rio, SOAPHTTPClient,jpeg;

type
  TForm1 
= class(TForm)
    HTTPRIO1: THTTPRIO;
    Button1: TButton;
    Image1: TImage;
    procedure Button1Click(Sender: TObject);
  
private
    procedure ByteArrayCompressedToImage(
const ByteArray :
      TByteDynArray);
    
{ Private declarations }
  
public
    
{ Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure CopyToStream( 
const InArray : TByteDynArray ; outStream : TStream );
var pTemp : Pointer;
begin
  pTemp :
= @InArray[0];
  outStream.Write( pTemp
^, Length(InArray));
end;


procedure TForm1.ByteArrayCompressedToImage( 
const ByteArray : TByteDynArray);
var MStream : TMemoryStream;

    Jpg : TJpegImage;
begin
  MStream :
= TMemoryStream.Create;
  CopyToStream( ByteArray, MStream );
  MStream.Position :
= 0;
  Jpg :
=TJpegImage.Create;
  
try
    Image1.Picture.Graphic:
=Jpg;       //設置圖片框的格式
    Image1.Picture.Graphic.LoadFromStream(MStream);       //將地址下的文件通過內存流寫入圖片框中
    Image1.Repaint;


  
finally
    MStream.Free;

  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  ByteArray : TByteDynArray;
begin
  ByteArray :
= (HTTPRIO1 as IQueryCapital).GetPic;
  ByteArrayCompressedToImage(ByteArray);
end;

end.
/////////////////////////////////////////////////////////////////////////////////////////////////////
//爲一個朋友面試做的小程序程序:
//通過WebServices 訪問服務器上的一張圖片
//開始的時候通過 服務器上加載 圖片文件,然後通過WebService 直接傳TPicture 對象到Client。發現這條路不行;
//後來想到用 WebService傳String到Client; 圖片文件轉成String成功。Client段接收WebService傳過來的String,數據出現丟失
//用 TByteDynArray 傳圖片文件成功!!!!!!!!

  





 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章