Uploading Datasnap Image

3

I need to transfer images from Android to the Server, tried the code below but is giving the error, I use Delphi Berlin:

Exception class EJNIException with message 'java.net.SocketException: sendto failed: EPIPE (Broken pipe)'.

Client side (Android)

I remember the photo is saved in sqllite.

procedure TDM.EnviaImagens;
var
  Fdq: TFDQuery;
  I: Integer;
  BS: TStream;
  cod: String;
begin
  Fdq := TFDQuery.Create(Self);
  Fdq.Connection := FreteappliteConnection;
  Fdq.SQL.Clear;
  Fdq.SQL.Add('select viagem, imagem from fotos');
  Fdq.Open;
  Fdq.First;
  if Fdq.RecordCount > 0 then
  begin
    for I := 0 to Fdq.RecordCount - 1 do
    begin
      Fdq.Edit;
      BS := Fdq.CreateBlobStream(Fdq.FieldByName('imagem'), bmReadWrite);
      cod := TNetEncoding.Base64.EncodeBytesToString(BS, BS.Size);
      if not(ModuloCliente.SrvServerMetodosClient.AgroInsereFotos
        (Fdq.FieldByName('viagem').AsInteger, cod) = True) then
      begin
        BS.Free;
        Fdq.Free;
        exit
      end
      else
      begin
        Fdq.Next;
        BS.Free;
      end;
    end;
  end;

end;

On the Server side I have the following code:

function TSrvServerMetodos.AgroInsereFotos(Viagem: Integer;
  Foto: String): Boolean;
var
  Fdq: TFDQuery;
  BS: TStream;
begin
  Result := False;
  try
    BS := TBytesStream.Create(TNetEncoding.Base64.DecodeStringToBytes(Foto));
    Fdq := TFDQuery.Create(Self);
    Fdq.Connection := FDConnection;
    Fdq.SQL.Clear;
    Fdq.SQL.Add
      ('insert into Agropecuaria_Viagens_Fotos (codigo, viagem, foto) values (:codigo, :viagem, :foto)');
    Fdq.ParamByName('codigo').AsInteger := Random(10);
    Fdq.ParamByName('viagem').AsInteger := Viagem;
    Fdq.ParamByName('foto').AsStream := BS;
    Fdq.ExecSQL;
    Fdq.Free;
    BS.Free;
    Result := True;
  Except
    Result := False;
    Fdq.Free;
    BS.Free;
  end;
end;
    
asked by anonymous 13.07.2017 / 13:30

1 answer

0

I was able to solve the problem using these two functions, thanks.

Server Side

function TSrvServerMetodos.AgroInsereFotos(const AArquivo: String; Size: Int64;
  Viagem: Integer; const ASetStream: TStream): Boolean;
var
  FileStream: TStream;
  Mem: TMemoryStream;
  Buffer: PByte;
  BufSize: Integer;
  BytesRead: Integer;
  Destino: String;
begin
  Result := True;
  BufSize := 1024;
  try
    Mem := TMemoryStream.Create;
    GetMem(Buffer, BufSize);
    try
      FileStream := ASetStream;
      FileStream.Position := 0;

      repeat
        BytesRead := FileStream.Read(Pointer(Buffer)^, BufSize);
        if (BytesRead > 0) then
          Mem.WriteBuffer(Pointer(Buffer)^, BytesRead);
      until (BytesRead < BufSize);
      Destino := 'C:\Users\Administrador\Desktop\datasnap\img\' + AArquivo;
      Mem.SaveToFile(Destino);
    finally
      FreeMem(Buffer, BufSize);
      FreeAndNil(Mem);
    end;

  Except
    on E: Exception do
      Result := False;
  end;
end;

Client Side

procedure TDM.EnviaImagens;
var
  Fdq: TFDQuery;
  I: Integer;
  fStream: TStream;
  Size: Int64;
  Arquivo: String;
begin
  Fdq := TFDQuery.Create(Self);
  Fdq.Connection := FreteappliteConnection;
  Fdq.SQL.Clear;
  Fdq.SQL.Add('select viagem, imagem from fotos');
  Fdq.Open;
  Fdq.First;
  if Fdq.RecordCount > 0 then
  begin
    for I := 0 to Fdq.RecordCount - 1 do
    begin
      Fdq.Edit;
      fStream := Fdq.CreateBlobStream(Fdq.FieldByName('imagem'), bmRead);
      Fdq.Post;
      Size := fStream.Size;
      fStream.Position := 0;
      Arquivo := IntToStr(Random(1000)) + '.jpg';
      if not(ModuloCliente.SrvServerMetodosClient.AgroInsereFotos(Arquivo, Size,
        Fdq.FieldByName('viagem').AsInteger, fStream) = True) then
      begin
        Fdq.Free;
        exit
      end
      else
      begin
        Fdq.Next;
      end;
    end;
    Fdq.SQL.Clear;
    Fdq.SQL.Add('delete from fotos');
    Fdq.ExecSQL;
  end;

end;
    
13.07.2017 / 20:52