0

我正在试验 MongoDB 和 Delphi ( mongo-delphi-driver )

虽然我能够使用底部的代码上传文件,但我正在努力将其从 MongoDB 下载回我的文件系统。

是否有人已经有可以显示给我的代码片段?

谢谢大家

uses
  ...
  MongoDB, MongoBson, GridFS;

...

procedure UploadFile();
const
  LOCAL_FILE_NAME = 'C:\local_file_name'; 
  REMOTE_FILE_NAME = 'remote_file_name';
var
  GridFSStoreFileSuccess: Boolean;
  myGridFS: TGridFS;
begin
  myGridFS := TGridFS.Create(mongo, db);
  try
    GridFSStoreFileSuccess := myGridFS.storeFile(LOCAL_FILE_NAME, REMOTE_FILE_NAME);
    if GridFSStoreFileSuccess then
      ShowMessage('File upload was successful!')
    else
      ShowMessage('File upload was *NOT* successful!');
  finally
    myGridFS.Free;
  end;
end;
4

1 回答 1

0

目前我也尝试将 mongodb 与 delphi 一起使用。这是满足您需求的片段

var
  GridFSTest : TGridFS;
  p : Pointer;
  stream : TMemoryStream;
  filename : string;
  f : TGridfile;
begin  
  // Create and link TGridFS to 'test' db
  GridFSTest := TGridFS.Create(Mongo,'test');
  stream := TMemoryStream.Create;

  // Find the image file... 
  f := GridFSTest.find('topo gigio.jpg');

  p := AllocMem(f.getLength);
  f.read(p,f.getLength);

  stream.Write(p^,f.getLength);
  stream.Position := 0;

  ClientDataSet1.Close;
  ClientDataSet1.CreateDataSet;
  ClientDataSet1.Insert;
  TBlobField(ClientDataSet1.FieldByName('immagine')).LoadFromStream(stream);
  ClientDataSet1.Post;

//  stream.SaveToFile('c:\a.jpg');

  stream.Free;
  FreeMem(p);
  GridFS.Free;
end;
于 2017-05-04T11:54:53.840 回答