2

I am trying to accept file uploads in a Delphi 7 Webbroker CGI.

I'm using Shiv Kumar's TMsMultipartParser, but I have a problem with Chrome. I can't access the parsed data (surprisingly, Explorer works fine).

This is my code:

with TMsMultipartFormParser.Create do
begin
    Parse(Request);

    lsExternalID:=ContentFields.Values['external_id'];

    if (lsExternalID='') then
        raise Exception.Create('No external ID');

    for i := 0 to Files.Count -1 do
    begin
        lsFileName:=files[i].FileName;
        //Rename file using external ID (not included for simplicity)
        Files[i].SaveToFile(lsFilename);
    end;
    Response.Content := 'OK';
    free;
end;

As suggested here, I tried to use http://www.mrsoft.org/Delphi/MultipartParser.pas but I can't compile it. It uses a unit called UniversalUtils that I can't find anywhere.

I know this is a very obsolete technology. Almost all references to it have already disappeared from the web (believe me, I have searched). Buy any help would be deeply appreciated.

Thanks.

Community
  • 1
  • 1
Tarrakis
  • 371
  • 3
  • 12
  • What does "can't access the parsed data" mean exactly? Do you get an error? The data isn't there as it should be? Is the exception being raised? You haven't specified a problem. – Ken White Nov 26 '14 at 17:35
  • 1
    just remove UniversalUtils from the uses. – bummi Nov 26 '14 at 17:38
  • Yes remove this unit - I accidentally moved some functions from there into this unit (which we use in our company)... I uploaded a new version of that file to my site which includes the missing functions. – mrabat Nov 27 '14 at 08:43
  • Thank you guys. @bummi, I got compiler error when I removed it. – Tarrakis Dec 02 '14 at 09:53
  • Thanks @mrabat, you're very kind. I tried it, but I still got an error with a call to "UTF8ToString". I then realized that I'm using a unit that is meant to adapt Shiv's multiparser to Unicode. I am using Delphi 7 with ANSI strings (no unicode support), therefore I don't really need it. Then I saw that I could use Delphi 7's TMultipartContentParser in ReqMulti.pas. I didn't find any example, but it wasn't hard to figure out. I had been using Shiv's Multiparser because the project had been started on Delphi 5, and there was no parser included. Thanks for your help! – Tarrakis Dec 02 '14 at 10:01

2 Answers2

3

I finally solved my problem, thanks to @mrabat. This project started in Delphi 5. It was later upgraded to Delphi 7 (it can't be upgraded further, because many parts can't support Unicode strings, we use ANSI). We were using Shiv's TMsMultipartParser because Delphi 5 didn't have any parser included. Delphi 7 has TMultipartContentParser in unit ReqMulti.pas, and it works perfectly.

For anyone that need an example, I'll post my working code:

with TMultipartContentParser.Create(Request) do
begin
    lsExternalID:=ContentFields.Values['external_id'];
    if (lsExternalID='') then
        raise Exception.Create('No external ID');

    for i := 0 to Request.Files.Count -1 do
    begin
        lsFileName:=Request.Files[i].FileName;
        //Rename file using external ID (not included for simplicity)
        TMemoryStream(Request.Files[i].Stream).SaveToFile(lsFilename);
    end;
    Response.Content := 'OK';
    Free;
end;
Tarrakis
  • 371
  • 3
  • 12
0

I wrote something similar once here: https://github.com/stijnsanders/xxm/blob/master/Delphi/common/xxmParams.pas#L159 but that may be tightly coupled with SplitHeaderValue that parses the header lines, and TStreamNozzle that throttles incoming data. (and TXxmReqPar... objects, and IXxmContext...)

(Of course you're warmly welcomed to accept file uploads with xxm...)

Stijn Sanders
  • 35,982
  • 11
  • 45
  • 67
  • Thanks Stijn, but it turns out my problem was quite simple. I wasn't using the correct multipart parser. – Tarrakis Dec 02 '14 at 10:04