gpt4 book ai didi

javascript - 使用javascript在couchdb中存储内联附件

转载 作者:行者123 更新时间:2023-11-30 18:31:52 26 4
gpt4 key购买 nike

我想将内联附件与新文档一起存储。任何机构都可以提供 java 脚本片段来存储内联附件。有什么方法可以在附加文件时提供 key 。

提前致谢

最佳答案

首先,阅读 CouchDB attachments文档。

例如:

  • 在文件 my_doc
  • 附加文件 hello.html
  • 包含内容 Hello, world

您使用 base64 对内容进行编码。 "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>

关于javascript - 使用javascript在couchdb中存储内联附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9430876/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com