Pouchdb 简明教程

PouchDB - Retrieving Attachment

你可以使用 getAttachment() 方法从 PouchDB 检索附件。此方法永远返回 blob 或缓冲区对象。

You can retrieve an attachment from PouchDB using the getAttachment() method. This method always returns blob or buffer objects.

Syntax

以下是 getAttachment() 的语法。我们必须向此方法传递文档 ID 和附件 ID。此方法还接受可选的回调函数。

Following is the syntax of the getAttachment(). To this method, we have to pass the document id and attachment id. This method also accepts an optional callback function.

db.getAttachment( docId, attachmentId, [callback] );

Example

以下是使用 getAttachment() 方法检索存储在 PouchDB 中的文档附件的示例。使用此代码,我们试图从文档 001 检索附件 att_1.txt

Following is an example of retrieving an attachment of a document stored in PouchDB, using getAttachment() method. Using this code, we are trying to retrieve an attachment att_1.txt from the document 001.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = new PouchDB('my_database');

//Retrieving an attachment from a document
db.getAttachment('001', 'att_1.txt', function(err, blob_buffer) {
   if (err) {
      return console.log(err);
   } else {
      console.log(blob_buffer);
   }
});

将上述代码保存在名为 Retrieve_Attachment.js 的文件中。打开命令提示符并使用 node 执行 JavaScript 文件,如下所示。

Save the above code in a file with the name Retrieve_Attachment.js. Open the command prompt and execute the JavaScript file using node as shown below.

C:\PouchDB_Examples >node Retrieve_Attachment.js

这会检索文档的附件,并如下所示显示在控制台中。

This retrieves the attachment of the document and displays on the console as shown below.

<Buffer 00>

Retrieving Attachment from a Remote Document

你还可以检索存在于存储在服务器上的(CouchDB)数据库中的文档附件。

You can also retrieve an attachment of a document existing in the database that is stored remotely on the server (CouchDB).

要执行此操作,您需要传入 CouchDB 中的数据库路径而不是数据库名称,该路径包含要读取的文档。

To do so, instead of a database name, you need to pass the path to the database in CouchDB, which contains the document that is to be read.

Example

假设 CouchDB 服务器中有一个名为 my_database 的数据库。然后,如果您使用 URL http://127.0.0.1:5984/_utils/index.html 验证 CouchDB 中的数据库列表,您将获得以下屏幕截图。

Suppose there is a database named my_database in the CouchDB server. Then, if you verify the list of databases in CouchDB using the URL http://127.0.0.1:5984/_utils/index.html you will get the following screenshot.

retrieving attachment from remote database

如果你选择名为 my_database 的数据库,你可以查看其内容,如下所示。

If you select the database named my_database, you can view its contents as shown below.

retrieving attachment

假设此文档中有一个附件,如下所示。

Suppose, there is an attachment in this document as shown below.

attachment

以下是针对存储在名为 my_database 的数据库中的文档 001 检索附件的示例,此数据库存储在 CouchDB 服务器中。

Following is an example of retrieving an attachment of the document 001 that exists in a database named my_database, which is stored in the CouchDB server.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = new PouchDB('http://localhost:5984/my_database');

//Retrieving an attachment from a document
db.getAttachment('001', 'att_1.txt', function(err, blob_buffer) {
   if (err) {
      return console.log(err);
   } else {
      console.log(blob_buffer);
   }
});

将上述代码保存在名为 Remote_Retrieve_Attachment.js 的文件中。打开命令提示符并使用 node 执行 JavaScript 文件,如下所示。

Save the above code in a file with the name Remote_Retrieve_Attachment.js. Open the command prompt and execute the JavaScript file using node as shown below.

C:\PouchDB_Examples >node Remote_Retrieve_Attachment.js

这会检索文档附件,并如下所示显示在控制台中。

This retrieves the document attachment and displays it on the console as shown below.

<Buffer 00>