I need to receive a csv text file that is being sent via a web app using an HTML Form tag.
Wagner pointed me to the code I needed - pasted below.
Could someone please describe to me how I use this? If I call this from a service, how do I define the service? What params are required and of what type?
Thanks,
Paul
uses {...}, Sparkle.Multipart.FormDataReader;
procedure ProcessRequest(const C: THttpServerContext);
var
Reader: TMultipartFormDataReader;
I: integer;
Part: TMultipartFormDataPart;
vres: string;
begin
Reader := TMultipartFormDataReader.Create(C.Request.ContentStream, C.Request.Headers.Get('content-type'));
try
vres := '';
while Reader.Next do
begin
Part := Reader.PartInfo;
vres := vres + 'Name: ' + Part.Name + #13#10;
if Part.FileName <> '' then
vres := vres + 'FileName: ' + Part.FileName + #13#10;
vres := vres + 'MediaType: ' + Part.MediaType + #13#10;
vres := vres + 'Charset: ' + Part.Charset + #13#10;
vres := vres + 'Content-Disposition: ' + Part.ContentDisposition + #13#10;
vres := vres + '=== Params ==='#13#10;
for I := 0 to Part.ParamCount - 1 do
vres := vres + Part.ParamName[I] + ': ' + Part.ParamValue[I] + #13#10;
if (Part.FileName <> '') and (Part.MediaType <> '') then
TFile.WriteAllBytes(
(TPath.Combine(ExtractFilePath(ParamStr(0)), TPath.GetFileName(Part.FileName))),
Reader.ContentAsBytes)
else
vres := vres + 'Content: ' + Reader.ContentAsString + #13#10;
vres := vres + #13#10;
end;
C.Response.StatusCode := 200;
C.Response.ContentType := 'text/plain;charset=UTF-8';
C.Response.Close(TEncoding.UTF8.GetBytes(vres));
finally
Reader.Free;
end;
end;