2012-02-24 3 views

Répondre

2

Pour commencer, lisez la documentation CouchDB attachments.

Par exemple:

  • Dans le document my_doc
  • Pour joindre un fichier hello.html
  • Avec un contenu Hello, world

Vous encodez le contenu avec base64. "Hello world" est "'SGVsbG8gd29ybGQ=".

Et vous créez un document comme celui-ci:

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

La seule partie difficile est l'encodage base64. Je suggère que vous utilisiez le script base64 inclus dans 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> 
Questions connexes