I'm using Delphi as a server to serve a number of different requests. All that are simple strings works fine, but I have some trouble recieving files.
All are implemented using a Webbroker service, so I get a method
WebModule1WebActionItem1Action(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
I have a /test method, with type=mtPost.
Inside of the action, I do the following:
MimeType := Request.ContentFields.Values['MimeType'];
for i := 0 to Request.Files.Count-1 do begin
// never entered
aFile := Request.Files.Items[i];
ms := TMemoryStream.Create;
aFile.Stream.Position := 0;
ms.CopyFrom(aFile.Stream, aFile.Stream.Size);
ms.SaveToFile(path+aFile.FileName);
ms.free;
end;
Apparently whatever I send is never recognized as files, but I dont know why. The HTML used to post, looks like this:
<form id="myForm" action="http://localhost:8080/test" method="post" enctype="multipart/form-data">
<input type="file" size="60" name="myfile">
<input type="text" size="10" name="mimetype" value="image/hest">
<input type="submit" value="upload">
</form>
I'd appriciate anybody telling me what could be wrong - and how to solve it. Basically I just need to be able to recieve 1 file at the time, including the mimetype (because I need to return it when I serve the file later)
I had the same problem and it took quite awhile to find the answer. TWebRequest does not process Multipart forms by default. Include the unit ReqMulti in your project and it will be extended to support multipart and suddenly the Request.Files object will actually have data in it.
As a side note, once you're using multipart forms Request.GetFieldByName doesn't seem to work anymore but Request.ContentFields.Values['fieldname'] does.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With