我写了一些关于附件的文档,希望很快会合并到 cradle 自述文件中。现在虽然这里是相关部分
附件
Cradle 支持写入、读取和删除附件。读取和写入操作可以是缓冲的或流式的
写作
您可以缓冲整个附件正文并将其作为单个请求一次发送。附件上传完成或出错后回调函数会触发
句法
db.saveAttachment(idData, attachmentData, callbackFunction)
示例
假设您要将文本文档保存为名称为“fooAttachment.txt”且内容为“Foo 文档文本”的附件
var doc = <some existing document>
var id = doc._id
var rev = doc._rev
var idAndRevData = {
id: id,
rev: rev
}
var attachmentData = {
name: 'fooAttachment.txt',
'Content-Type': 'text/plain',
body: 'Foo document text'
}
db.saveAttachment(idAndRevData, attachmentData, function (err, reply) {
if (err) {
console.dir(err)
return
}
console.dir(reply)
})
流媒体
您可以使用读取流来上传附件正文,而不是先缓冲整个正文。流式上传完成或发生错误后会触发回调函数
句法
var doc = savedDoc // <some saved couchdb document which has an attachment>
var id = doc._id
var rev = doc._rev
var idAndRevData = {
id: id,
rev: rev
}
var attachmentData = {
name: attachmentName // something like 'foo.txt'
'Content-Type': attachmentMimeType // something like 'text/plain', 'application/pdf', etc.
body: rawAttachmentBody // something like 'foo document body text'
}
var readStream = fs.createReadStream('/path/to/file/')
var writeStream = db.saveAttachment(idData, attachmentData, callbackFunction)
readStream.pipe(writeStream)
流式上传完成后,回调函数将触发
示例
将位于路径 './data/bar.pdf' 的名为 'bar.pdf' 的 pdf 文件附加到现有文档
var path = require('path')
var fs = require('fs')
// this document should already be saved in the couchdb database
var doc = {
_id: 'fooDocumentID',
_rev: 'fooDocumentRev'
}
var idData = {
id: doc._id,
rev: doc._rev
}
var filename = 'bar.pdf' // this is the filename that will be used in couchdb. It can be different from your source filename if desired
var filePath = path.join(__dirname, 'data', 'bar.pdf')
var readStream = fs.createReadStream
// note that there is no body field here since we are streaming the upload
var attachmentData = {
name: 'fooAttachment.txt',
'Content-Type': 'text/plain'
}
db.saveAttachment(idData, attachmentData, function (err, reply) {
if (err) {
console.dir(err)
return
}
console.dir(reply)
}, readStream)
阅读
缓冲的
您可以缓冲整个附件并一次性接收。下载完成或发生错误后,回调函数将触发。回调中的第二个参数将是附件的二进制数据
句法
db.getAttachment(documentID, attachmentName, callbackFunction)
示例
假设您要读回以名称“foo.txt”保存的附件
var doc = <some saved document that has an attachment with name *foo.txt*>
var id = doc._id
var attachmentName = 'foo.txt'
db.getAttachment(id, attachmentName, function (err, reply) {
if (err) {
console.dir(err)
return
}
console.dir(reply)
})
流媒体
您也可以流式传输附件。如果附件很大,则将其流式传输以限制内存消耗可能很有用。下载流完成后,回调函数将触发。请注意,只有一个错误参数传递给回调函数。如果下载附件时出错,则错误为 null 或错误对象。没有像缓冲读取示例中那样包含附件数据的第二个参数
句法
var readStream = db.getAttachment(documentID, attachmentName, callbackFunction)
示例
假设您要读回一个名为“foo.txt”的附件。但是附件 foo.txt 非常大,因此您希望将其流式传输到磁盘而不是将整个文件缓冲到内存中
var doc = <some saved document that has an attachment with name *foo.txt*>
var id = doc._id
var attachmentName = 'foo.txt'
var downloadPath = path.join(__dirname, 'foo_download.txt')
var writeStream = fs.createWriteStream(downloadPath)
var readStream = db.getAttachment('piped-attachment', 'foo.txt', function (err) { // note no second reply paramter
if (err) {
console.dir(err)
return
}
console.dir('download completed and written to file on disk at path', downloadPath)
})
readStream.pipe(writeStream)
移除
您可以使用 _id 和附件名称删除上传的附件
句法
db.removeAttachment(documentID, attachmentName, callbackFunction)
示例
假设您要删除以名称“foo.txt”保存的附件
var doc = <some saved document that has an attachment with name *foo.txt*>
var id = doc._id
var attachmentName = 'foo.txt'
db.removeAttachment(id, attachmentName, function (err, reply) {
if (err) {
console.dir(err)
return
}
console.dir(reply)
})