2017-08-08 48 views
0
GridFSFileInfo fileInfo; 
var options = new GridFSFindOptions 
{ 
    Limit = 1 
}; 
FilterDefinition<GridFSFileInfo> filterDefinition = Builders<GridFSFileInfo>.Filter 
    .Where(ee => ee.Id.Equals(new ObjectId(fileId))); 
using (var cursor = _mongoDbHelper.Bucket.Find(filterDefinition, options)) 
{ 
    fileInfo = cursor.ToList().FirstOrDefault(); 
}    
byte[] file = _mongoDbHelper.Bucket.DownloadAsBytes(new ObjectId(fileId)); 
UploadedFile uploadedFile = new UploadedFile(fileInfo.Filename, file);     
return uploadedFile; 

我使用MongoDB的V2在C#中,我想通過給FILEID但在網上downaload的byte []和另外獲取文件名:MongoDB的V2 C#獲取文件名

_mongoDbHelper.Bucket.Find(filterDefinition, options) 

其拋我一個例外:

System.InvalidOperationException

和消息是:

{document} .Id不受支持。

但是GridFSFileInfo具有屬性名稱ID。 我如何找到文件名?

回答

1

我發現不同的回答這個問題:

var stream = _mongoDbHelper.Bucket.OpenDownloadStream(new ObjectId(fileId)); 
      var fileName = stream?.FileInfo.Filename;