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;
我在c#中使用mongoDB v2,我想下载byte []并另外通过给出fileId获取文件名但是在行中:
_mongoDbHelper.Bucket.Find(filterDefinition, options)
它给我一个例外:
System.InvalidOperationException
,消息是:
{document} .Id不受支持。
但GridFSFileInfo有一个属性名称ID。 我怎样才能找到文件名?
答案 0 :(得分:1)
我找到了不同的答案:
var stream = _mongoDbHelper.Bucket.OpenDownloadStream(new ObjectId(fileId));
var fileName = stream?.FileInfo.Filename;