hi,

there is one procsedure I've found in the message archive to extract a MIME-encoded file on the server's side, but it is less general to use as one may wish, because it can only handle forms with only one element on it.

I 'ver changed something so that the sending form may contain more values than only the file. (see below for procedure extractfile ... )

this procedure can be called by
extractfile(clientcnx, filename);
where filename will be set by the procedure too.

But there is one problem in my code. To determine where the file starts and ends I'm searching for the strings #13+#10+#13+#10 for the start and #13+#10+'------------------' for the end. But what if the submitted file contains these strings ? Is there another solution which will fix the problem ?

Greetings, Stefan

Procedure extractfile(ClientCnx : TMyHttpConnection; var filename: string);
var len,Blen: integer;
   temp: string;
   startoffile: string[4];
   endoffile: string[20];
   myStream: TMemoryStream;
   i, count: integer;
Begin
                   startoffile:= ''; count:= 0;
//searches for the string "Content-Type:",to determine, where in the MIME-encoded form the file is Len := Pos('Content-Type:', clientcnx.FPostedDataBuffer); //get start marker for start of file
                   For i:= len to length(clientcnx.FPostedDataBuffer) do
                   begin
                   count:= count+1;
                   if length(startoffile) = 4 then delete(startoffile,1,1);
startoffile:= startoffile+clientcnx.FPostedDataBuffer[i];
                   if startoffile = #13+#10+#13+#10 then break;
                   end;
                   len:= len+count;

                   //search the end marker in the file
                   count:=0;
                   For i:= len to length(clientcnx.FPostedDataBuffer) do
                   begin
                   count:= count+1;
                   if length(endoffile) = 20 then delete(endoffile,1,1);
                   endoffile:= endoffile+clientcnx.FPostedDataBuffer[i];
                   if endoffile = #13+#10+'------------------' then break;
                   end;

                   //now the lenght of the file must be
                   Blen := count-20;

                   //get filename from multipart form
Filename := AnsiStrPos(clientcnx.FPostedDataBuffer,'filename="');

                   //switch to a string for delete functions
                   Temp := Filename;
                    //remove the 'filename="' section
                   Delete(Temp, 1, 10);
//remove everything after the '"'+#13+#10 so we get just the filename
                   Delete(Temp, pos('"'+#13+#10, Temp), Length(Temp));

                   //only if a file was submitted, we want to save it
                   if temp<>'' then
                   begin
                       MyStream := TMemoryStream.Create;
MyStream.Write(clientcnx.Fposteddatabuffer[len],blen);
                       MyStream.Seek(0,0);
MyStream.SaveToFile(ExtractFilePath(ParamStr(0)) +ExtractFileName(Temp));
                       MyStream.Free;
                   end;

                     //don't forget  to give filename the right value
                   filename:= temp;
end;

------------------------------

Message: 2
Date: Thu, 2 Jun 2005 19:54:04 +0200
From: "Francois PIETTE" <[EMAIL PROTECTED]>
Subject: Re: [twsocket] Upload file using http protocol
To: "ICS support mailing" <twsocket@elists.org>
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; format=flowed; charset="iso-8859-1";
        reply-type=original

You have to format your posted data according to the MIME standard. This has been discussed several times before. I suggest you scan the mailing list archives to find what has been said on the subject.
There is a searchable archive here: http://marc.theaimsgroup.com/?l=twsocket
You can download the archive here : http://www.elists.org/mailman/listinfo/twsocket

--
[EMAIL PROTECTED]
http://www.overbyte.be

----- Original Message ----- From: "robertovalente" <[EMAIL PROTECTED]>
To: <twsocket@elists.org>
Sent: Thursday, June 02, 2005 6:42 PM
Subject: [twsocket] Upload file using http protocol


How to upload a file if i have a client application in local computer and a
WINCGI application on a Web Site ?
(Cliente side and Server side)

Thanks

  Roberto



--
To unsubscribe or change your settings for TWSocket mailing list
please goto http://www.elists.org/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to