Storing images in Mysql and treating by file size

Asked

Viewed 1,846 times

2

I was able to store images in my bank, the problem now is just to treat so that the images only enter the bank to a certain size, assuming: 800kb. How to proceed?

Follows the code:

Botão Gravar:

procedure TfrmFoto.SpeedButton1Click(Sender: TObject);
var
  Jpeg : TJpegImage;
begin
 if OPPicture.execute then
    Image1.Picture.LoadFromFile(OPPicture.FileName);

  if OPPicture.FileName <> '' then
  begin
    Jpeg := TJpegImage.Create;
    Jpeg.LoadFromFile(OPPicture.FileName);

    dmconn.zqFoto.Close;
    dmconn.zqFoto.SQL.Clear;
    dmconn.zqFoto.SQL.Add('INSERT INTO fotos(nome, fotoAnt) VALUES (:pnome, :pfotoant)');
    dmconn.zqFoto.ParamByName('pnome').AsString := edtNome.Text;
    dmconn.zqFoto.ParamByName('pfotoant').Assign(Jpeg);
    dmconn.zqFoto.ExecSQL;

    Jpeg.Free;
  end;
end;

And here’s the other button:

Botão Ler:

procedure TfrmFoto.SpeedButton2Click(Sender: TObject);
var
  sqltexto : string;
  Jpeg : TJpegImage;
  Stream : TStream;
begin
  dmconn.zqFoto.Close;
  dmconn.zqFoto.SQL.Clear;
  dmconn.zqFoto.SQL.Add('SELECT nome, fotoAnt FROM fotos WHERE nome = :pnome');
  dmconn.zqFoto.ParamByName('pnome').AsString := edtNomeLer.Text;
  dmconn.zqFoto.Open;

  Stream := dmconn.zqFoto.CreateBlobStream(dmconn.zqFoto.Fields[1],BMREAD);

  try
    Jpeg := TJpegImage.Create;
    Jpeg.LoadFromStream(Stream);
    Image2.Picture.Assign(Jpeg);
  except
   // Jpg.Free;
  end;
  • Idea: It would not be easier to upload the image to some site directory and store in the database only the path of this directory combined with the file name?

  • nor scroll friend, it would have to be in the same bank, and it is not site, it is a system for desktop, hence I need to show the stored image, however it can not be big, it would have to be up to 800kb in size, all I want is to just treat this.

1 answer

3


I think checking the file size with a File.Lenght resolves:

procedure TForm1.Button1Click(Sender: TObject);
var
myFile: file of Byte; // Utilizando file of byte, o tratamento do arquivo
// pode ser genérico, sem se preocupar se o mesmo é texto ou exe
myFileSize: Longint; // LongInt para garantir que arquivos muito longos
// também sejam tratados
begin
if OpenDialog1.Execute then
begin
AssignFile(myFile, OpenDialog1.FileName); // Cria um ponteiro
Reset(myFile); // Abre o arquivo como somente leitura
myFileSize := FileSize(myFile); // Obtém o tamanho do arquivo
ShowMessage (IntToStr (FileSize (myFile)); // Exibe o tamanho
end;
end;

end.
  • And how it would look in the code friend?

  • Test this code, I hope it’s last. :)

  • Perfect friend, thank you so much! D

  • Plan, good luck with your project !!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.