unit Push_main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPServer, IdCustomHTTPServer,
IdHTTPServer, StdCtrls, Contnrs;
type
TForm1 =
class(TForm)
Server: TIdHTTPServer;
Active: TCheckBox;
Port: TEdit;
procedure ServerCommandGet(AThread: TIdPeerThread;
ARequestInfo: TIdHTTPRequestInfo;
AResponseInfo: TIdHTTPResponseInfo);
procedure ServerCreatePostStream(ASender: TIdPeerThread;
var VPostStream: TStream);
procedure FormCreate(Sender: TObject);
procedure ActiveClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function HtmlForm(msg:String='
'):
string;
var T:
String;
begin
T:='
<html><body><h1>File Upload</h1><hr>';
If not(msg='
')
then T:=t+msg+'
<hr>';
T:=t+'
<form action="/upload/" method=post enctype="multipart/form-data"><input type=file name=file><input type=submit value=Upload></form></body></html>';
Result:=t;
end;
procedure TForm1.ServerCommandGet(AThread: TIdPeerThread;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
Var PostedFile:TMemoryStream;
begin
If ARequestInfo.Document='
/'
Then begin
With AResponseInfo
do begin
ContentText:=HtmlForm;
WriteContent;
end;
end else if ARequestInfo.Document='
/upload/'
then begin
PostedFile:=TMemoryStream.Create;
Try
Try
PostedFile.LoadFromStream(ARequestInfo.PostStream);
PostedFile.SaveToFile('
.\'+(DateToStr(now)+'
'+TimeToStr(now)+'
'+AThread.Connection.Socket.Binding.PeerIP));
With AResponseInfo
do begin
ContentText:=HtmlForm('
Upload Successful!');
WriteContent;
end;
except
With AResponseInfo
do begin
ContentText:=HTMLForm('
Upload Error!');
WriteContent;
end;
end;
finally
PostedFile.Free;
end;
end;
end;
procedure TForm1.ServerCreatePostStream(ASender: TIdPeerThread;
var VPostStream: TStream);
begin
VPostStream:=TMemoryStream.Create;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
LongTimeFormat:='
hhmmss';
ShortDateFormat:='
yyMMdd';
end;
procedure TForm1.ActiveClick(Sender: TObject);
begin
Try
If Active.Checked
then begin
Server.DefaultPort:=StrTointDef(Port.Text,80);
end;
Server.Active:=Active.Checked;
Finally
Active.Checked:=Server.Active;
end;
end;
end.