使用Java脚本在Couchdb中存储内联附件 [英] Storing inline attachments in couchdb using java script

查看:76
本文介绍了使用Java脚本在Couchdb中存储内联附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将内联附件与新文档一起存储。任何机构都可以提供Java脚本代码段来存储内联附件。并且在附加文件时有什么方法可以提供密钥。

I want to store inline attachments along with new document. Can any body provide the java script snippet to store inline attachments. And is there any way to provide a key while attaching file.

预先感谢

推荐答案

首先,请阅读 CouchDB附件文档。

例如:


  • 文档 my_doc

  • 附加文件 hello.html

  • 内容为您好,世界

  • In document my_doc
  • To attach a file hello.html
  • With content Hello, world

您可以使用base64对内容进行编码。 Hello world 'SGVsbG8gd29ybGQ =

You encode the content with base64. "Hello world" is "'SGVsbG8gd29ybGQ=".

然后您创建一个像这样的文档:

And you create a document like this:

{ "_id": "my_doc",
, "_attachments":
  { "hello.html":
    { "content_type": "text/html"
    , "data": "'SGVsbG8gd29ybGQ="
    }
  }
}

唯一困难的部分是base64编码。我建议您使用CouchDB中包含的base64脚本。

The only difficult part is base64 encoding. I suggest that you use the base64 script included inside CouchDB.

<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>

这篇关于使用Java脚本在Couchdb中存储内联附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆