簡體   English   中英

如何使用delphi mongo-delphi-driver.GridFS.pas從MongoDB下載文件?

[英]How can I download a file from MongoDB using delphi mongo-delphi-driver.GridFS.pas?

我正在嘗試使用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;

目前,我還嘗試將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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM