cross socket multipart
需要4个单元:
Net.CrossHttpParams.pas
Net.CrossHttpUtils.pas
Utils.DateTime.pas
Utils.Utils.pas
procedure TCrossHttpServer.TriggerPostDataBegin(const AConnection : ICrossHttpConnection); var LRequest: TCrossHttpRequest; LMultiPart: THttpMultiPartFormData; LStream: TStream; begin LRequest := AConnection.Request as TCrossHttpRequest; case LRequest.BodyType of btMultiPart: begin LStream := TBytesStream.Create(nil); FreeAndNil(LRequest.FBody); LRequest.FBody := LStream; // if (FStoragePath <> '') and not TDirectory.Exists(FStoragePath) then // TDirectory.CreateDirectory(FStoragePath); // // LMultiPart := THttpMultiPartFormData.Create; // LMultiPart.StoragePath := FStoragePath; // LMultiPart.AutoDeleteFiles := FAutoDeleteFiles; // LMultiPart.InitWithBoundary(LRequest.RequestBoundary); // FreeAndNil(LRequest.FBody); // LRequest.FBody := LMultiPart; end; btUrlEncoded: begin LStream := TBytesStream.Create; FreeAndNil(LRequest.FBody); LRequest.FBody := LStream; end; btBinary: begin LStream := TBytesStream.Create(nil); FreeAndNil(LRequest.FBody); LRequest.FBody := LStream; end; end; if Assigned(FOnPostDataBegin) then FOnPostDataBegin(Self, AConnection); end; procedure TCrossHttpServer.TriggerPostData(const AConnection : ICrossHttpConnection; const ABuf: Pointer; const ALen: Integer); var LRequest: TCrossHttpRequest; begin LRequest := AConnection.Request as TCrossHttpRequest; case LRequest.GetBodyType of btMultiPart: (LRequest.Body as TStream).Write(ABuf^, ALen); // (LRequest.Body as THttpMultiPartFormData).Decode(ABuf, ALen); btUrlEncoded: (LRequest.Body as TStream).Write(ABuf^, ALen); btBinary: (LRequest.Body as TStream).Write(ABuf^, ALen); end; if Assigned(FOnPostData) then FOnPostData(Self, AConnection, ABuf, ALen); end; procedure TCrossHttpServer.TriggerPostDataEnd(const AConnection : ICrossHttpConnection); var LRequest: TCrossHttpRequest; LUrlEncodedStr: string; LUrlEncodedBody: THttpUrlParams; begin LRequest := AConnection.Request as TCrossHttpRequest; case LRequest.GetBodyType of btUrlEncoded: begin SetString(LUrlEncodedStr, MarshaledAString((LRequest.Body as TBytesStream).Memory), (LRequest.Body as TBytesStream).Size); LUrlEncodedBody := THttpUrlParams.Create; LUrlEncodedBody.Decode(LUrlEncodedStr); FreeAndNil(LRequest.FBody); LRequest.FBody := LUrlEncodedBody; end; btBinary, btMultiPart: begin (LRequest.Body as TStream).Position := 0; end; end; if Assigned(FOnPostDataEnd) then FOnPostDataEnd(Self, AConnection); end;