3

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)

4
  • Try a FileStream instead of aMemoryStream
    – Hidden
    Nov 14, 2013 at 12:20
  • That wont make a difference, since I'm never getting to that part of the code. My issue is with finding out/getting the files, not storing it. Nov 15, 2013 at 10:29
  • Hello, have you got a valid solution?
    – img.simone
    Jun 26, 2017 at 15:11
  • We actually ended up using a variant of Svip's suggestion, where we parse the multipart form request ourselves. The ReqMulti didn't add the files as expected. Jun 29, 2017 at 12:16

2 Answers 2

11

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.

3
  • Sounds great - I'll try that, when I get back from my vacation :) Jul 15, 2014 at 20:03
  • I found this answer by chance, it saved my day! Thanks! So easy! Jul 6, 2017 at 16:49
  • This an underrated comment. This solution totally works in 10.2 Tokyo and 10.3 Rio
    – jxn
    Dec 5, 2018 at 15:09
0

You could simply just read the raw request and split on the first double line break:

Data := Request.ReadUnicodeString(Request.ContentLength);
// Where StrAfter is a function that splits on the first occurrence
// of the first parameter.
Data := Trim(StrAfter(#13#10#13#10, Data));

Of course, that only allows you to read one file. Maybe you should read the headers to see the exact length of each file to split Data between them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.