Jump to content

Recommended Posts

Posted

I need to post files to client. I am not find such event in unigui. I am catch event in

procedure TIdCustomHTTPServer.DoCommandGet where

TMethod(FOnCommandGet).Data is TUniExtServer

 

How to get current TUniExtSession from TUniExtServer and while such requests

 

localhost:8077/tmp/myfile.jpg?cmd=1&id=2

 

not translated by TUniExtServer to procedure TIdExtSession.HandleRequest()?

 

 

Please help me. I need drop file from brauser to desctop in google chrome i altready write javascript and now need catch file request to generate file dinamically from database. For this i need MainDataModule from UniSession in TIdCustomHTTPServer.DoCommandGet or translation localhost:8077/tmp/myfile.jpg?cmd=1&id=2 to TIdExtSession.HandleRequest or event in unigui for user manual params and request handling

 

 

 

Please help :(

Posted

for handling requests like localhost:8077?cmd=1&id=12375

 

define project define GUR if main database module named MainData from file dtHtmlMain

 

In module ExtHTTPServer; in uses after implementation insert uses

 

implementation

uses
{$IFDEF GUR}
   UniGUIApplication,dtHtmlMain,
{$ENDIF}
 

 

and change TIdExtSession.HandleRequest(

 

 

procedure TIdExtSession.HandleRequest(ARequest: TIdHTTPRequestInfo; AResponse: TIdHTTPResponseInfo);
(* added *)
{$WARNINGS OFF}
(* added *)
  function CheckIfFileIsModified(FileName: string): Boolean;
  const
    FCompareDateFmt = 'yyyymmddhhnnss';
  var
    FFileDateTime: TDateTime;
  begin
    Result := True;
    if (ARequest.RawHeaders.Values['if-Modified-Since'] <> '') then begin
      FFileDateTime := FileDateToDateTime(FileAge(FileName));
      Result := not SameText(FormatDateTime(FCompareDateFmt, FFileDateTime),
        FormatDateTime(FCompareDateFmt, StrInternetToDateTime(ARequest.RawHeaders.Values['if-Modified-Since'])));
    end;
  end;

  function TryToServeFile: boolean;
  var
    FileName: string;
    FileDateTime: TDateTime;
  begin
    FileName := ExtractFilePath(ParamStr(0));
    FileName := StringReplace(FileName, ExtractFileDrive(FileName), '', []);
    if (Length(ARequest.Document) > 1) and (ARequest.Document[1]in ['/', '\']) then
      FileName := FileName + Copy(ARequest.Document, 2, MaxInt)
    else
      FileName := FileName + ARequest.Document;
    FileName := ExpandFilename(FileName);
    if FileExists(FileName) then
    begin
      Result := True;
      if CheckIfFileIsModified(FileName) then
      begin
        AResponse.ContentStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
        AResponse.FreeContentStream := True;
        AResponse.ContentLength := AResponse.ContentStream.Size;
        FileDateTime := FileDateToDateTime(FileAge(FileName));
        AResponse.LastModified := FileDateTime;
        AResponse.ContentType  := FileType2MimeType(FileName);
      end
      else
        AResponse.ResponseNo := 304; //Not Modified, use cache version
    end
    else
    begin
(* added *)
      AResponse.ResponseNo := 404; //Not found
(* added *)
      Result := false;
    end;
  end;

  function TryToServeDiskFile(FileName: String): boolean;
  var
    FileDateTime: TDateTime;
  begin
    if FileExists(FileName) then
    begin
      Result := True;
      if CheckIfFileIsModified(FileName) then
      begin
        AResponse.ContentStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
        AResponse.FreeContentStream := True;
        AResponse.ContentLength := AResponse.ContentStream.Size;
        FileDateTime := FileDateToDateTime(FileAge(FileName));
        AResponse.LastModified := FileDateTime;
        AResponse.ContentType  := FileType2MimeType(FileName);
      end
      else
        AResponse.ResponseNo := 304; //Not Modified, use cache version
    end
    else
    begin
(* added *)
      AResponse.ResponseNo := 404; //Not found
(* added *)
      Result := false;
    end;
  end;

(* added *)
{$WARNINGS ON}
(* added *)

type
  MethodCall = procedure of object;
var
  PageMethod : TMethod;
  MethodCode : pointer;
{$IFDEF GUR}
var
  AID: variant;
  ACmd: Integer;
  AFileName: String;
{$ENDIF}
begin
(* deleted *)
//CurrentFCGIThread := Self;
(* deleted *)

(* added *)
  SetCurrentFCGIThread(Self);
(* added *)
  FCurrentRequest   := ARequest;
  FCurrentResponse  := AResponse;
//  FCurrentResponse.CacheControl:='no-cache';

(* deleted *)
{
  if ARequest.Cookies.GetCookieIndex(0, 'FCGIThread') = -1 then
    with AResponse.Cookies.Add do begin
      CookieName := 'FCGIThread';
      Value      := SessionID;
      ARequest.Cookies.AddSrcCookie(CookieText);
    end;

  AResponse.ContentType := 'text/html'; // this changes charset in D2011 see #0000531
}
(* deleted *)
  if Length(ARequest.Params.Values['cmd'])>0 then
    begin
    Response := '';
    end;

  Response := '';
  FParams.DelimitedText := FCurrentRequest.UnParsedParams;
  if BeforeHandleRequest then
    if PathInfo = '' then
      begin
{$IFDEF GUR}
      if Length(ARequest.Params.Values['cmd'])>0 then
        begin
        ACmd := StrToInt(ARequest.Params.Values['cmd']);
        case ACmd of
          0: // file
            begin
            AID := StrToFloat(ARequest.Params.Values['id']);
            if TMainData(TUNIGUISession(Self).UniMainModule).PutToTmpFile(AID, AFileName) then //; if FileExists(FILE_ROOT + ARequestInfo.Document) then
              begin
              TryToServeDiskFile(AFileName); //AResponse.ServeFile(AContext, AFileName);
              end;
            end;
        end;
        end //;
      else
{$ENDIF}
        Home;
      end
    else
    begin
      MethodCode := MethodAddress(PathInfo);
      if MethodCode <> nil then
      begin
        PageMethod.Code := MethodCode;
        PageMethod.Data := Self;
        try
{$IFDEF GUR}
          if Length(ARequest.Params.Values['cmd'])>0 then
            begin
            ACmd := StrToInt(ARequest.Params.Values['cmd']);
            case ACmd of
              0: // file
                begin
                AID := StrToFloat(ARequest.Params.Values['id']);
                if TMainData(TUNIGUISession(Self).UniMainModule).PutToTmpFile(AID, AFileName) then //; if FileExists(FILE_ROOT + ARequestInfo.Document) then
                  begin
                  TryToServeDiskFile(AFileName); //AResponse.ServeFile(AContext, AFileName);
                  end;
                end;
            end;
            end //;
          else
{$ENDIF}
          MethodCall(PageMethod); // Call published method
        except
          on E : Exception do
            OnError(E, E.Message, PathInfo, FCurrentRequest.UnParsedParams);
        end;
      end
      else
      begin
        if not TryToServeFile then
          OnNotFoundError;
      end;
    end;
  AfterHandleRequest;
  if not Assigned(AResponse.ContentStream) and (Response <> '') and (AResponse.ResponseNo <> 304) then
    FCurrentResponse.ContentText := Response;
end;
 

 

 

In tunihtmlframe you can define startdrag function

 

<script type="text/javascript">

 

function dragStart(evt)
{
 
    fID = MyForm.MyLabel.text;

  files = "application/octet-stream:"+fName+":"+document.location+"?cmd=0&id="+fID;
  event.dataTransfer.setData("DownloadURL",files);
  return true;
}
 

 

and register procedure

 

var box = document.getElementById(MYForm.MyGrid.id);

 box.addEventListener("dragstart", dragStart, false);
</script>

 

 

Its way to dragdrop files from tunidbgrid in browser to desctop Work only in googlechrome.

 

Possible unigui developers included event in MainModule  on with done like

 

    function TUniguiSession.BeforeHandleRequest : boolean; override;

begin

   if Assigned (MainDataModule.FOnBeforeHanle) then

     Result := MainDataModule.FOnBeforeHanle(CurrentRequest ,CurrentResponse)

   else inherited;

)

 

 

 

end;
 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...