所以我不知道我的标题是否足够清楚,我想做的是,在我的 Meteor 应用程序中,有一个工具可以将文件上传到我的 MongoDB 中,并输出一个我可以使用它的 URL显示为 src 的示例 然而,我创建了我的收藏:
export const Files = new Mongo.Collection('files');
和一个 addFile 函数:
export const addFile = (nameArg: String, dataURL: String) => {
Files.insert({
_id: uuid(),
name: nameArg,
url: dataURL
});
console.log("file "+nameArg+" added");
};
然后我使用react-jsonschema-form
包来显示文件表单
<Form
schema={{
type: 'object',
properties: {
file: {
type: 'string',
title: 'Upload an image to get its URI'
}
}
}}
uiSchema={{
file: {
'ui:widget': 'file'
}
}}
onChange={this.onChange}
liveValidate
>
<div />
</Form>
我的 onChange 函数:
onChange = (e: { formData: { file: string } }) =>{
const [dataType, nameBis,] = e.formData.file.split(';');
const name = nameBis.split('=')[1];
Promise.resolve().then(addFile(name, e.formData.file));
}
我想使用地址 sush 作为 window.location.host+'/file/'+name
以便能够使用该图像。
我相信这是可能的,但是如何实现呢?
感谢您的所有回复:)
最佳答案
使用 cfs:standard-packages
和 cfs:filesystem
包,这非常有可能并且以安全的方式进行。坦白说,我没有深入探讨你的问题。
这是一个漂亮的包装。您可以阅读上面的内容here packages in depth .
创建文件系统集合的代码
var imageStore = new FS.Store.FileSystem("YOUR_COLLECTION_NAME");
YOUR_COLLECTION_NAME = new FS.Collection("YOUR_COLLECTION_NAME", {
stores: [imageStore]
});
简而言之。这些文件将保存在 bundle/
位置的同级位置:cfs/files/YOUR_COLLECTION_NAME
。有关文件的信息将保存在集合 cfs.YOUR_COLLECTION_NAME.filerecord
中,临时位置将用于集合 cfs._tempstore.chunks
的内部用途,如下所示。
如果您使用上述包保存任何文件。元数据将保存在 cfs.YOUR_COLLECTION_NAME.filerecord
中,如下所示
{
"_id" : "TBmxbsL2cMCM2tEc7",
"original" : {
"name" : "photo.jpg",
"updatedAt" : ISODate("2017-07-06T12:54:50.115Z"),
"size" : 2261,
"type" : "image/jpeg"
},
"uploadedAt" : ISODate("2017-07-08T06:58:32.433Z"),
"copies" : {
"YOUR_COLLECTION_NAME" : {
"name" : "photo.jpg",
"type" : "image/jpeg",
"size" : 2261,
"key" : "YOUR_COLLECTION_NAME-TBmxbsL2cMCM2tEc7-photo.jpg",
"updatedAt" : ISODate("2017-07-08T06:58:32.475Z"),
"createdAt" : ISODate("2017-07-08T06:58:32.475Z")
}
}
}
在客户端,您可以使用
获取文档文件的链接YOUR_COLLECTION_NAME.find({"_id": "TBmxbsL2cMCM2tEc7"}).url();
此 URL 是基于 token 的文件安全链接,而不是服务器的直接位置。您可以像普通集合一样为下载和内容设置额外的允许/拒绝设置。希望这有帮助!
关于javascript - 在 MongoDB 中存储 dataURL 以通过本地 URL (JS) 访问它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45017531/