multipart form data not working with files

Giganews Newsgroups
Subject: multipart form data not working with files
Posted by:  Cameron Hart (ne…@news.co.nz)
Date: Fri, 14 Sep 2007

Hi Group

When using TIdMultipartformdata there is a problem if you add a file field
which is not the first field in the form.

The multipart form is cleared and then the file data is added.  for example
add a "username" field, a "password" field, and then a file field and during
output of the data to HTTP stream it only contains the file data.  the other
fields are overwritten.

if you add the file first, then the username and password fields the correct
data is output.

This problem is caused by the use of TIdStreamHelper.ReadBytes in
TIdMultiPartFormDataStream.IdRead to write the file stream to the multipart
form data stream.

i have gone for an easy fix because i dont want to change ReadBytes without
knowing what it affects.  I have instead read the file into FInternalBuffer
and then copied to VBuffer by using CopyTIdBytes

the following code is from TIdMultiPartFormDataStream.IdRead.  note the
commented line is change to the following 2 lines.

if Assigned(FInputStream) and (LTotalRead < ACount) then begin
      //LCount := TIdStreamHelper.ReadBytes(FInputStream,VBuffer, ACount -
LTotalRead, LBufferCount);
      {change by Cameron Flow begins}
      LCount := TIdStreamHelper.ReadBytes(FInputStream,FInternalBuffer,
ACount - LTotalRead, 0);
      CopyTIdBytes(FInternalBuffer, 0, VBuffer, LBufferCount, LCount);
      {change by Cameron Flow ends}
      if LCount < (ACount - LTotalRead) then begin
        FInputStream.Position := 0;
        FInputStream := nil;
        Inc(FCurrentItem);
        SetLength(FInternalBuffer, 0);
        AppendString(FInternalBuffer, #13#10);
      end;

I am hoping someone responsible for Indy can verify that this code works or
provide alternative code.

regards,
Cameron

Replies