在MongoDB中存储dataURL以通过本地URL(JS)访问它 [英] Store a dataURL in MongoDB to access it via local URL (JS)
问题描述
所以我不知道我的标题是否足够清楚,我想要做的是,在我的Meteor应用程序中,有一个工具可以将文件上传到我的MongoDB中,并输出一个我可以使用它的URL显示为
的src但是,我创建了我的Collection:
So I don't know if my title is clear enough, what I want to do is, in my Meteor App, to have a tool that upload a file into my MongoDB, and output an URL that I could use it for example to display as the src of an Yet, I create my Collection:
export const Files = new Mongo.Collection('files');
和一个addFile函数:
and a addFile function :
export const addFile = (nameArg: String, dataURL: String) => {
Files.insert({
_id: uuid(),
name: nameArg,
url: dataURL
});
console.log("file "+nameArg+" added");
};
然后我使用 react-jsonschema-form
包显示文件表格
Then I use the react-jsonschema-form
package to display a file 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函数:
My onChange function :
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
可以使用此图片。
我相信它有可能,但是如何?
And I'd like to use an address sush as window.location.host+'/file/'+name
to be able to use this image.
I believe it to be possible, but how ?
感谢所有回复:)
推荐答案
使用软件包 cfs:standard-packages
和 cfs:filesystem,这是非常有可能的。
。坦率地说,我没有深入探讨你的问题。
This is very much possible and in a secure way using packages cfs:standard-packages
and cfs:filesystem
. Frankly speaking, I did not go in depth for your question.
这是一个漂亮的包。您可以在上面阅读这里的深度包。
It is a beautiful package. You can read about above here packages in depth.
代码创建文件系统
var imageStore = new FS.Store.FileSystem("YOUR_COLLECTION_NAME");
YOUR_COLLECTION_NAME = new FS.Collection("YOUR_COLLECTION_NAME", {
stores: [imageStore]
});
简而言之。这些文件将保存在兄弟姐妹的捆绑/
位置 cfs / files / YOUR_COLLECTION_NAME
。有关该文件的信息将保存在集合 cfs.YOUR_COLLECTION_NAME.filerecord
中,临时位置将用于内部目的,集合 cfs._tempstore.chunks
如下。
In Short. The files will be saved at the sibling to bundle/
location at cfs/files/YOUR_COLLECTION_NAME
. Information about the file will be saved in the collection cfs.YOUR_COLLECTION_NAME.filerecord
and temporary location will be used for internal purpose with collection cfs._tempstore.chunks
as below.
如果使用上述包保存任何文件。元数据将保存在 cfs.YOUR_COLLECTION_NAME.filerecord
中,如下所示
If you save any file using above package. The metadata will be saved inside the cfs.YOUR_COLLECTION_NAME.filerecord
as below
{
"_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:T BmxbsL2cMCM2tEc7})。url();
此网址是基于令牌的文件安全链接,而非直接位置到您的服务器。您可以像下载普通集合一样为下载和设置其他允许/拒绝设置。希望这有帮助!
This url is token based safe link to the file instead of direct location to your server. You can set additional allow/deny settings for download ans stuff just like normal collections. Hope this helps!
这篇关于在MongoDB中存储dataURL以通过本地URL(JS)访问它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!