Back to Pouchdb

Get Attachment

docs/_includes/api/get_attachment.html

9.0.03.3 KB
Original Source

{% include anchor.html edit="true" title="Get an attachment" hash="get_attachment" %} {% highlight "js" %} db.getAttachment(docId, attachmentId, [options], [callback]) {% endhighlight %} Get attachment data. ### Options * options.rev: as with get(), you can pass a rev in and get back an attachment for the document at that particular revision. #### Example Usage: Get an attachment with filename 'att.txt' from document with ID 'doc': {% include code/start.html id="get_att1" type="callback" %} {% highlight "js" %} db.getAttachment('doc', 'att.txt', function(err, blobOrBuffer) { if (err) { return console.log(err); } // handle result }); {% endhighlight %} {% include code/end.html %} {% include code/start.html id="get_att1" type="async" %} {% highlight "js" %} try { const blobOrBuffer = await db.getAttachment('doc', 'att.txt'); } catch (err) { console.log(err); } {% endhighlight %} {% include code/end.html %} {% include code/start.html id="get_att1" type="promise" %} {% highlight "js" %} db.getAttachment('doc', 'att.txt').then(function (blobOrBuffer) { // handle result }).catch(function (err) { console.log(err); }); {% endhighlight %} {% include code/end.html %} Get an attachment with filename 'att.txt' from document with ID 'doc', at the revision '1-abcd': {% include code/start.html id="get_att2" type="callback" %} {% highlight "js" %} db.getAttachment('doc', 'att.txt', {rev: '1-abcd'}, function(err, blobOrBuffer) { if (err) { return console.log(err); } // handle result }); {% endhighlight %} {% include code/end.html %} {% include code/start.html id="get_att2" type="async" %} {% highlight "js" %} try { const blobOrBuffer = await db.getAttachment('doc', 'att.txt', {rev: '1-abcd'}); } catch (err) { console.log(err); } {% endhighlight %} {% include code/end.html %} {% include code/start.html id="get_att2" type="promise" %} {% highlight "js" %} db.getAttachment('doc', 'att.txt', {rev: '1-abcd'}).then(function (blobOrBuffer) { // handle result }).catch(function (err) { console.log(err); }); {% endhighlight %} {% include code/end.html %} #### Response type: The response will be a Blob object in the browser, and a Buffer object in Node.js. See blob-util for utilities to transform Blobs to other formats, such as base64-encoded strings, data URLs, array buffers, etc. #### Inline base64 attachments You can specify {attachments: true} to most "read" operations, such as get(), allDocs(), changes(), and query(). The attachment data will then be included inlined in the resulting doc(s). However, it will always be supplied as base64. For example: {% highlight "js" %} { "_attachments": { "att.txt": { "content_type": "text/plain", "digest": "d5ccfd24a8748bed4e2c9a279a2b6089", "data": "SXMgdGhlcmUgbGlmZSBvbiBNYXJzPw==" } }, "_id": "mydoc", "_rev": "1-e147d9ec9c85139dfe7e93bc17148d1a" } {% endhighlight %} For such APIs, when you don't specify {attachments: true}, you will instead get metadata about the attachments. For example: {% highlight "js" %} { "_attachments": { "att.txt": { "content_type": "text/plain", "digest": "d5ccfd24a8748bed4e2c9a279a2b6089", "stub": true } }, "_id": "mydoc", "_rev": "1-e147d9ec9c85139dfe7e93bc17148d1a" } {% endhighlight %} This "summary" operation may be faster in some cases, because the attachment itself does not need to be read from disk.