How can I send file as result in TkbmMWEventHTTPService.Get?

Home Forums kbmMW How can I send file as result in TkbmMWEventHTTPService.Get?

Viewing 2 reply threads
  • Author
    Posts
    • #54592
      VadimMest
      Participant

      Hello

      I use TkbmMWEventHTTPService as REST server for mobile application (on Android).

      I put some images on server root folder and tried to get it from WEB browser. WEB browser doesn’t recognize result as binary file.  How do I fill result for send file as it does usual HTTP WEB server?

      I use this code

      function TkbmMWHTTPService.kbmMWEventHTTPServiceGet(const Func: string; const ClientIdent: TkbmMWClientIdentity; const AURL: string;
      Args: TkbmMWVariantList): Variant;

      var
      f: TFileStream;
      s: TStringStream;
      s1: String;
      begin
      f := nil;
      s := nil;
      try
      try
      s1 := ExtractFileDir(ParamStr(0)) + ‘\root’ + AURL;
      if FileExists(s1) then
      begin
      f := TFileStream.Create(s1, fmOpenRead);
      Result := StreamToVariant(f);
      end
      else
      begin
      s := TStringStream.Create;
      s.WriteString(‘<h1>Mobile Waiter Server</h1>’);
      s.WriteString(Format(‘Page %s not found’, [AURL]));
      Result := StreamToVariant(s);

      end;
      finally
      if f <> nil then
      FreeAndNil(f);
      if s <> nil then
      FreeAndNil(s);
      end;
      except

      end;
      end;

       

      Thanks

       

      • This topic was modified 5 years, 8 months ago by VadimMest.
    • #54594
      VadimMest
      Participant

      I’ve done it

      I added Self.SetResponseMimeType(

       

      f := TFileStream.Create(s1, fmOpenRead);
      ext := UpperCase(ExtractFileExt(AURL));
      if leftStr(ext, 1) = ‘.’ then
      ext := copy(ext, 2, length(ext) – 1);
      if ext = ‘APK’ then
      Self.SetResponseMimeType(‘application/vnd.android.package-archive’)
      else
      Self.SetResponseMimeType(GetMimeTypeExt(ext));
      Result := StreamToVariant(f);

       

      • This reply was modified 5 years, 8 months ago by VadimMest.
    • #54641
      kimbomadsen
      Keymaster

      Hi,
      You can also add the mimetype for APK to the service definitions mimetype list.

      Eg:

      TkbmMWHTTPServiceDefinition(server.RegisterService...).MimeTypes.Values['APK']:=‘application/vnd.android.package-archive’;
      

      Then you can simply use

      Result:=self.HTTPResponseFromFile(yourAPKfilepath);
      

      best regards

      Kim/C4D

      • This reply was modified 5 years, 8 months ago by kimbomadsen.
Viewing 2 reply threads
  • You must be logged in to reply to this topic.