Link to home
Start Free TrialLog in
Avatar of faustomen
faustomen

asked on

How can I send a MemoryStream to a cgi application ?

Hi,

I am using Tidhttp.post('www.host.com/my.cgi',Image_in_MemoryStream) to send an image to cgi application.
How can I put this image in a memory stream variable in the cgi application (my.cgi)?

Thanks
Avatar of luwo
luwo


hi fausomen, maybe this works:

//1x idhttp-component,
//1x Memo-component

procedure TForm1.Button1Click(Sender: TObject);
var src,cont,tmp:Tmemorystream;
    ds,boundary:string;
    //helping procedure to write string with linefeed...
       procedure wr(const txt:string);
       var ds:string;
       begin
         ds:=txt+#13#10;
         src.Write(ds[1],length(ds));
       end;
begin

src  :=Tmemorystream.Create;
cont :=Tmemorystream.Create;


boundary:='---------------------------thisStringHasToBeUNIQUE';

//your http-content-type is multipart
idhttp1.Request.ContentType:='multipart/form-data; boundary='+boundary;

//the binary-file:
wr('--'+boundary);
wr('Content-Disposition: form-data; name="userfile"; filename="C:\originalfilename.bmp"');
wr('Content-Type: application/octet-stream');
wr('');

  //integrate the bin-data...
  tmp:=tmemorystream.Create;
  tmp.LoadFromFile('c:\binary.bmp');
  tmp.seek(0,0); src.CopyFrom(tmp,tmp.size);
  tmp.free;

wr(''); //last-linefeed is absolutely necessary!

//close your multipart...
wr('--'+boundary+'--');
wr('');

 IdHTTP1.Post('http://www.host.com/response.php?var1=content1',src,cont);

 cont.seek(soFromBeginning,0);
 memo1.Lines.LoadFromStream(cont);

src.free;
cont.free;
end;


//your response.php-file could be like this:
<?
$tmpfilename=$_FILES["userfile"][tmp_name];
$orgfilename=$_FILES["userfile"][name];

system('cp '.$tmpfilename.' /tmp/'.$orgfilename);

/*
the structure of _$FILES["blalaber"] is
Array
(
    [name] => testfile.zip
    [type] => application/x-zip-compressed
    [tmp_name] => /tmp/phppZxrmP
    [error] => 0
    [size] => 29868
)
*/
?>
---------------
hope, this is what r you searching for...

luwo
ASKER CERTIFIED SOLUTION
Avatar of luwo
luwo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of faustomen

ASKER

Hi luwo,

I need to use a Delphi cgi application to receive image.
My client application is ready to send the image (using IdHTTP1.Post) but I dont know how to transfer the stream (from client application) to a variable MemoryStream in the cgi application.

Thanks


faustomen:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:
       to accept luwo's answer
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

kacor
EE Cleanup Volunteer