/// <summary> /// 上传文件 /// </summary> /// <param name="doc"></param> /// <param name="options"></param> /// <returns></returns> public async Task <string> UpLoadFile(MongoDbFileDoc doc, GridFSUploadOptions options = null) { var bucket = new GridFSBucket(GetMongoDatabase(), new GridFSBucketOptions()); var id = await bucket.UploadFromBytesAsync(doc.FileName, doc.FileBytes, options); return(id.ToString()); }
/// <summary> /// 下载文件 /// </summary> /// <param name="id"></param> /// <returns></returns> public async Task <MongoDbFileDoc> DownLoadFile(string id) { var model = new MongoDbFileDoc(); var filter = Builders <GridFSFileInfo <ObjectId> > .Filter.Eq(x => x.Id, new ObjectId(id)); var bucket = new GridFSBucket(GetMongoDatabase(), new GridFSBucketOptions()); var fileInfo = await bucket.FindAsync(filter); var file = await fileInfo.FirstOrDefaultAsync(); if (file != null) { model.FileName = file.Filename; model.FileId = id; model.FileBytes = await bucket.DownloadAsBytesAsync(new ObjectId(id)); } return(model); }