FileUpload

i have made a form Uploader and i want resive the form-data info:

curl -X 'POST' \
  'http://localhost:2001/afs/am/queryService/ReceiveDocument' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F 'Input=@afscrcode.jpg;type=image/jpeg'

Input and type

[HttpPost]
function ReceiveDocument(Value: TStream; *[FromBodyAttribute]* Input:string):string;

Witch Attribute ?

MySolution

function TDataManager.StreamToFile(Data:TStream): string;
var
LInput : TMemoryStream;
FData : TBytes;
Name:string;
Zeile1,
Kopf,s,
SName : string;
Fertig : Boolean;
Buffer : char;
i : int64;
begin
result:='';
if (Data=nil) then Exit;
//
try
//
// auch geladen
//
LInput := TMemoryStream.Create;
try
Fertig:=False;
Kopf:='';
Data.Position := 0;
while (Data.Position<Data.Size) and (fertig=False) do begin
Data.ReadData(Buffer,1);
Kopf:=Kopf+Buffer;
fertig:=pos(#13#10#13#10,Kopf)>0;
end;
//
i:=pos(#13#10,Kopf);
Zeile1:=Copy(Kopf,1,i-1);
//
if fertig=False then Data.Position := 0;
{
------WebKitFormBoundary5sYneBasWsrubxk5
Content-Disposition: form-data; name="Input"; filename="afscrcode.jpg"
Content-Type: image/jpeg

}
i:=Pos('filename="',Kopf);
if i>-1 then begin
s:=Copy(Kopf,i+10,255);
s:=Copy(s,1,POS('"',s)-1);
Name:=s;
end;
if Trim(Name)='' then Name:='undefiniert.xxx';
//
// am Ende steht #13#10 + die 1.Zeile mit -- und #13#10
//
LInput.CopyFrom(Data,Data.Size-Data.Position-Length(Zeile1)-6);
if Name=ExtractFileName(Name) then begin
// Verzeichnis ?
sName:=ExtractFileDir(ParamStr(0))+TPath.DirectorySeparatorChar+'upload'+TPath.DirectorySeparatorChar;
// MkDir(sName);
Name:=sName+Name;
end;
//LInput.Position:=0;
LInput.SaveToFile(Name);
//
result:=Name;
finally
LInput.Free;
end;
finally
end;
end;

1 Like

You can also use built-in multipart reader as an option:

https://doc.tmssoftware.com/biz/sparkle/guide/server.html#handling-multipart-content

how i integrat this in xdata Server ?

Just create a method that receives a TStream and operate directly on it.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.