Witam
Znalazlem gdzies na necie taki oto kodzik:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
IdBaseComponent,IdContext, IdComponent, IdCustomTCPServer, IdCustomHTTPServer,
IdHTTPServer, StdCtrls;
type
TForm1 = class(TForm)
IdHTTPServer1: TIdHTTPServer;
procedure IdHTTPServer1CommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo;
AResponseInfo: TIdHTTPResponseInfo);
procedure FormCreate(Sender: TObject);
procedure IdHTTPServer1CreatePostStream(AContext: TIdContext;
var VPostStream: TStream);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses IdHTTPHeaderInfo;
{$R *.DFM}
function HtmlForm:string;
begin
Result:=
'<html><head><title>Upload</title></head><body>'+
'<center><h1>File Upload</h1><hr>'+
'<form action="/upload/" method=post enctype="multipart/form-data">'+
'<input type=file name=file><input type=submit value=Upload></form>'+
'</center></body></html>';
end;
function HTMLMessage(msg:String):string;
begin
Result:=
'<html><head><title>Upload</title></head><body>'+
'<center><h1>File Upload</h1><hr>'+msg+'<hr>'+
'<a href=/>Click here to continue</a></center></body></html>';
end;
procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
Var TheFile:TMemoryStream;
FN:String;
begin
If ARequestInfo.Document='/' Then begin
With AResponseInfo do begin
ContentText:=HtmlForm;
WriteContent;
end;
end else if ARequestInfo.Document='/upload/' then begin
TheFile:=TMemoryStream.Create;
try
try
TheFile.LoadFromStream(ARequestInfo.PostStream);
TheFile.SaveToFile('.\'+DateToStr(now)+' '+TimeToStr(now)+' '+ARequestInfo.RemoteIP+' '+FN);
With AResponseInfo do begin
ContentText:=HtmlMessage('Upload Successful!');
WriteContent;
end;
except
With AResponseInfo do begin
ContentText:=HTMLMessage('Upload Error!');
WriteContent;
end;
end;
finally
TheFile.Free;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
LongTimeFormat:='hhmmss';
ShortDateFormat:='yyMMdd';
end;
procedure TForm1.IdHTTPServer1CreatePostStream(AContext: TIdContext;
var VPostStream: TStream);
begin
VPostStream:=TMemoryStream.Create;
end;
end.
ktory stawia serwer http z mozliwoscia wgrania do niego pliku z przegladarki
Plik sie przesyla ale mam kilka pytan i problemow:
1.da sie jakos przerobic to zeby pokazywal sie jakis progress(% i kb/s)?
2.jesli zamiast przegladarki chce uzyc IdHTTPClient czy da sie przesylac plik nie z clienta do serwera ale na odwrot? (poniewaz jedna osoba moze nie miec zewnetrznego ip a wysylany jest do niej plik)
3.jesli przesylam plik za pomoca powyzszego kodu to na serwerze zostaje on zapisany z dodatkowymi informacjami typu
-----------------------------7d696c11505b0
Content-Disposition: form-data; name="file"; filename="C:\test.txt"
Content-Type: text/plain
ORYGINALNA_ZAWARTOŚĆ_PLIKU
-----------------------------7d696c11505b0--
jak wiec zapisac go na dysku bez tych informacji?
Ps.Interesuje mnie tylko przesylanie za pomoca http (nie ftp itp)
Z gory dzekuje za kazda pomoc