Witam
Piszę prosty serwerek z wykorzystaniem TidHTTPServer. Mam problem z uploadem plików do serwera, tzn. upload działa ale plik zapisany na serwerze jest większy od wysłanego przez przeglądarkę.
Okazuje się że doklejony zostaje nagłówek. W jaki sposób „wyłuskać” sam plik bez nagłówka?
Proszę o jakieś wskazówki.
Oto kod:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPServer,
IdCustomHTTPServer, IdHTTPServer;
type
TForm1 = class(TForm)
IdHTTPServer1: TIdHTTPServer;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure IdHTTPServer1CommandGet(AThread: TIdPeerThread;
ARequestInfo: TIdHTTPRequestInfo;
AResponseInfo: TIdHTTPResponseInfo);
procedure IdHTTPServer1CreatePostStream(ASender: TIdPeerThread;
var VPostStream: TStream);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
case IdHTTPServer1.Active of
True : begin
Button1.Caption := 'Aktywuj';
IdHTTPServer1.Active := False;
end;
False: begin
Button1.Caption := 'Deaktywuj';
IdHTTPServer1.Active := True;
end;
end;
end;
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(AThread: TIdPeerThread;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
Var TheFile:TMemoryStream;
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('wynikowy.txt');
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.IdHTTPServer1CreatePostStream(ASender: TIdPeerThread;
var VPostStream: TStream);
begin
VPostStream := TMemoryStream.Create;
end;
end.
Plik tekstowy z dołączonym nagłówkiem wygląda tak:
-----------------------------7d696c11505b0
Content-Disposition: form-data; name="file"; filename="C:\Documents and Settings\Pirek\Pulpit\test.txt"
Content-Type: text/plain
ORYGINALNA_ZAWARTOŚĆ_PLIKU
-----------------------------7d696c11505b0--
Zawartość nagłówka jest zmienna (zależnie od przeglądarki, rodzaju uploadowanego pliku) wiec zwykłe obcięcie początku i końca raczej odpada. Nie bardzo już wiem jak to ugryźć.