以下示例代码:
using (var ms = new MemoryStream())
{
var artifactContent = Encoding.UTF8.GetBytes("content of some file/upload/etc as string");
ms.Write(artifactContent, 0, artifactContent.Length);
// ms.Lenght > 0
var gridFsCreateOptions = new MongoGridFSCreateOptions {ContentType = "text/plain"};
var gridFsFileInfo = mongoGridFsDatabase.GridFS.Upload(ms, "Test.txt", gridFsCreateOptions);
// stream is not uploaded and gridFsFileInfo.Lenght == 0
}
Run Code Online (Sandbox Code Playgroud)
该fs.files集合被填充,但fs.chunks集合为空。
我的代码有什么问题?我在 Win8 上使用 MongoDB 2.4.7 和来自 NuGet 的官方 C# 驱动程序。
据我了解,当你在GridFS中输入内容时,它会被引入2个不同的集合中.一个用于原始数据块,另一个用于元数据文件.
根据我对MongoDB文档的理解,您只能从GridFS中检索带有id或名称的文档.
var gs = new mongodb.GridStore(db, "test.png", "w", {
"content_type": "image/png",
"metadata":{
"author": "Daniel"
},
"chunk_size": 1024*4
Run Code Online (Sandbox Code Playgroud)
});
那么如果我想从GridFS获取文档的子集怎么办?例如,如果我想要所有GridStore:
元数据:{作者:"丹尼尔"}
为什么我不能使用标准的mongo查询{field:somevalue}并以这种方式检索文档?
有谁知道如何做到这一点?我在node.js上使用javascript API.
我们想使用 MongoDB GridFS 和 angular js 在 .net core 中创建一个名为 CDN 的解耦文件服务器。
从各种来源我尽力解决这个问题。最后我们做到了。
for some reason I keep getting a "TypeError: GridFsStorage is not a constructor" error. I have no idea why its giving me this error since I'm just following the official documentation.
Storage and upload
conn.once('open', ()=> {
gfs = Grid(conn.db, mongoose.mongo)
gfs.collection('uploads')
})
const storage = new GridFsStorage({
url: DBURI,
file: (req, file)=> {
return new Promise((resolve,reject)=> {
crypto.randomBytes(16, (err, buf)=> {
if(err) {
return reject(err)
}
const filename = buf.toString('hex') + path.extname(file.originalname);
const fileInfor = {
filename: filename,
bucketName: …Run Code Online (Sandbox Code Playgroud)