Storing images in MySQL and dealing with file size

2

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

Follow 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;
    
asked by anonymous 11.07.2014 / 15:48

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.
    
11.07.2014 / 15:56