I'm experimenting with MongoDB and Delphi (mongo-delphi-driver)
我正在试验MongoDB和Delphi(mongo-delphi-driver)
While I was able to upload a file using the code at bottom, I'm struggling to download it back from MongoDB to my filesystem.
虽然我能够使用底部的代码上传文件,但我很难将它从MongoDB下载回我的文件系统。
Does someone has already a code snippet that can show to me?
有人已经有一个可以显示给我的代码片段吗?
Thank you all in advance
谢谢大家
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;
1 个解决方案
#1
0
Currently I also try to use mongodb with delphi. This is a snippet for your needs
目前我也尝试使用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;
#1
0
Currently I also try to use mongodb with delphi. This is a snippet for your needs
目前我也尝试使用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;