Javascript 使用java脚本在couchdb中存储内联附件
我想将内联附件与新文档一起存储。任何主体都可以提供java脚本片段来存储内联附件。还有,在附加文件时是否有办法提供密钥 提前感谢首先,请阅读文档 例如:Javascript 使用java脚本在couchdb中存储内联附件,javascript,couchdb,Javascript,Couchdb,我想将内联附件与新文档一起存储。任何主体都可以提供java脚本片段来存储内联附件。还有,在附加文件时是否有办法提供密钥 提前感谢首先,请阅读文档 例如: 在文档中my\u doc 附加文件hello.html 内容Hello,world 您使用base64对内容进行编码“你好世界”是“'SGVsbG8gd29ybGQ=“” 然后创建如下文档: { "_id": "my_doc", , "_attachments": { "hello.html": { "content_type"
- 在文档中
my\u doc
- 附加文件
hello.html
- 内容
Hello,world
“'SGVsbG8gd29ybGQ=“
”
然后创建如下文档:
{ "_id": "my_doc",
, "_attachments":
{ "hello.html":
{ "content_type": "text/html"
, "data": "'SGVsbG8gd29ybGQ="
}
}
}
唯一困难的部分是base64编码。我建议您使用CouchDB中包含的base64脚本
<html>
<head>
<script src="/_utils/script/base64.js"></script>
</head>
<body>
The Base64 of "Hello world" is:
<script>
var hello = "Hello world"
var encoded = Base64.encode(hello)
document.write(encoded)
</script>
<p>
A document with this attachment is:<br>
<script>
var doc = { "_id":"my_doc" }
doc._attachments = {}
doc._attachments["hello.html"] = {}
doc._attachments["hello.html"].content_type = "text/html"
doc._attachments["hello.html"].data = Base64.encode("Hello world")
document.write(JSON.stringify(doc))
</script>
</body>
</html>
“Hello world”的Base64是:
var hello=“hello world”
var encoded=Base64.encode(你好)
文件。写入(编码)
带有此附件的文档是:
var doc={“\u id”:“我的\u doc”}
文件.\u附件={}
文档附件[“hello.html”]={}
文档附件[“hello.html”].content\u type=“text/html”
doc._attachments[“hello.html”].data=Base64.encode(“hello world”)
document.write(JSON.stringify(doc))