Forms to Upload Documents and Run Scripts
Often you will submit data to a server in TDataSets using TServerRequest components. Simple enough.
But there are some situations where you want to upload one or more files too, run a server script to process those files, then caputure the server results to display a message or utilize some returned values.
Solution
The easiest way (but not the only one), is to use a THTMLForm and place a TFileComboBox on that form, let the user pick a file, and then call the THTMLForm.submit function to do the upload.
You can set the output of THTMLForm to a TBrowser, and it will display the crude results in HTML format. Easy enough.
Hiding the Output
If you don’t wish to make the ugly HTML returned results, you can make the TBrowser invisible with browser.Visible := False.
If you do that, you will want to use some way to see when the browser has the results. The easiest way is to set the TBrowser’s OnLoad to a procedure which processes the incoming data. Note, it will be wrapped with some HTML junk you must discard.
There is a problem which pops up, the TBrowser.DocumentText is not always available to read. So you must wrap calls to read it in Try / Except pairs to handle the unreadable times. I’ll give an example later.
Programmatic Creation Finally, I noticed an oddness when I tried to create the THTMLForm and TBrowser in code and not the IDE. It didn’t work initially.
It turns out, this is one of the cases where you have to set the TBrowser’s Name property. Often you can get away without doing that, but the logic of HTMLForm depends on it and silently discards data if it’s not set..
The following simple program shows most of these concepts done in code.
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
browser : TBrowser;
htmlform1 : THTMLFOrm;
procedure newdata( Sender : TObject );
public
end;
var
Form1: TForm1;
implementation
procedure TForm1.Button1Click(Sender: TObject);
begin
browser := TBrowser.Create( form1 );
Browser.parent := form1;
Browser.Name := 'browser'; // tical - or it won't work
browser.OnLoad := Newdata;
browser.Visible := False;
htmlform1 := THTMLForm.Create( form1 );
htmlform1.parent := form1;
htmlform1.url := 'data';
// attach the output to a virtual browser
htmlform1.Output := browser;
htmlform1.submit;
end;
procedure TForm1.NewData( Sender : TObject );
var
s : string;
begin
try
// this assignment will fail initially, but succeed when the doument is transferred
s := browser.documenttext;
showmessage('change:'+s );
except
end;
end;