2012-02-24 54 views

回答

2

首先,閱讀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>