Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js CouchDB/Cradle如何添加图像?_Node.js_Couchdb_Imagesource_Cradle - Fatal编程技术网

Node.js CouchDB/Cradle如何添加图像?

Node.js CouchDB/Cradle如何添加图像?,node.js,couchdb,imagesource,cradle,Node.js,Couchdb,Imagesource,Cradle,所以基本上我想让用户在注册时可以选择上传图片。我永远不知道从哪里开始。我知道CouchDB支持附件,但它在摇篮中究竟是如何工作的呢 我在Cradle的文档中找到了以下代码 saveAttachment: function (/* id, [rev], attachmentName, contentType, dataOrStream */) { 所以我知道它可以保存附件。那么我该如何通过图像呢?我假设在html中,我必须使用 form(action='/upload', enctype='mu

所以基本上我想让用户在注册时可以选择上传图片。我永远不知道从哪里开始。我知道CouchDB支持附件,但它在摇篮中究竟是如何工作的呢

我在Cradle的文档中找到了以下代码

saveAttachment: function (/* id, [rev], attachmentName, contentType, dataOrStream */) {
所以我知道它可以保存附件。那么我该如何通过图像呢?我假设在html中,我必须使用

form(action='/upload', enctype='multipart/form-data', method='post')
input(type='file', name='upload')
input(type='submit', value='Upload')
但我该怎么办呢?这一步不会将映像保存在服务器上的某个位置。然后,我是否需要获取图像的地址并将其传递给摇篮,以将其保存为CouchDB数据库中的附件


如果你能帮助我,请提前感谢

您需要从表单获取传入流,然后通过将流发送到CouchDB

将流发送到摇篮可能是最简单的一点。此示例显示如何使用本地文件执行此操作:

db.saveAttachment( 
    doc.id, 
    doc.rev, 
    attachmentId,
    mimetype, 
    fs.createReadStream(path),
    function( err, data ){
        console.log(data);
    }
);
在我看来,更棘手的是管理传入的文件。它们以多部分流的形式到达,而不是保存到文件中。如果您使用的是Connect或Express,我倾向于直接或间接地将代码外包给

我当前的连接表单代码可以概括为:

req.form.complete(function(err, fields, files){
    if ( err ) // handle err
    else
    {
        db.saveAttachment( 
            doc.id, 
            doc.rev, 
            attachmentId,
            mimetype, 
            fs.createReadStream(files.name),
            function( err, data ){
                console.log(data);
            }
        );
    }
});
这并不是速度的最佳选择,因为它在磁盘上创建了一个实际的文件,而不是将数据从一个地方流到另一个地方,但它很方便,并且可能满足许多用例


如果您正在处理图像上载,您应该注意的另一个包是node.js wrapper for ImageMagick,正如您从名称中所期望的那样。

您需要从表单中获取传入流,然后通过向CouchDB发送流

将流发送到摇篮可能是最简单的一点。此示例显示如何使用本地文件执行此操作:

db.saveAttachment( 
    doc.id, 
    doc.rev, 
    attachmentId,
    mimetype, 
    fs.createReadStream(path),
    function( err, data ){
        console.log(data);
    }
);
在我看来,更棘手的是管理传入的文件。它们以多部分流的形式到达,而不是保存到文件中。如果您使用的是Connect或Express,我倾向于直接或间接地将代码外包给

我当前的连接表单代码可以概括为:

req.form.complete(function(err, fields, files){
    if ( err ) // handle err
    else
    {
        db.saveAttachment( 
            doc.id, 
            doc.rev, 
            attachmentId,
            mimetype, 
            fs.createReadStream(files.name),
            function( err, data ){
                console.log(data);
            }
        );
    }
});
这并不是速度的最佳选择,因为它在磁盘上创建了一个实际的文件,而不是将数据从一个地方流到另一个地方,但它很方便,并且可能满足许多用例


如果您正在处理图像上载,您应该注意的另一个软件包是node.js wrapper for ImageMagick,正如您从名称中所期望的那样。

FYI,对于未来的读者,调用参数自那时起已经更改,因此这似乎不再有效。检查源代码,因为文档中没有说明如何使用它。

FYI,对于未来的读者,调用参数自那时起已经更改,因此这似乎不再有效。检查源代码,因为文档中没有描述如何使用它。

我写了一些关于附件的文档,希望这些文档很快会合并到支架自述中。现在,这里是相关的部分

附件 支架支持写入、读取和删除附件。读写操作可以是缓冲的,也可以是流式的

书写 您可以缓冲整个附件正文,并将其作为单个请求一次性发送。附件上载完成或发生错误后,将触发回调函数

语法

db.saveAttachment(idData, attachmentData, callbackFunction)
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)
db.getAttachment(documentID, attachmentName, callbackFunction)
var readStream = db.getAttachment(documentID, attachmentName, callbackFunction)
db.removeAttachment(documentID, attachmentName, callbackFunction)
示例 假设要将文本文档保存为名为“fooAttachment.txt”且内容为“Foo document text”的附件

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)
})
阅读 缓冲 您可以缓冲整个附件并一次接收所有附件。下载完成或出现错误后,回调函数将启动。回调中的第二个参数是附件的二进制数据

语法

db.saveAttachment(idData, attachmentData, callbackFunction)
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)
db.getAttachment(documentID, attachmentName, callbackFunction)
var readStream = db.getAttachment(documentID, attachmentName, callbackFunction)
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.getAttachment(id, attachmentName, function (err, reply) {
  if (err) {
    console.dir(err)
    return
  }
  console.dir(reply)
})
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)
})
示例 假设您想读回名为“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)
示例 假设您要删除以“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)
})
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)
})
var doc=
变量id=单据编号
var attachmentName='foo.txt'
db.removeAttachment(id、attachmentName、函数(err、reply){
如果(错误){
console.dir(错误)
返回
}
console.dir(回复)
})

我写了一些关于附件的文档,希望这些文档很快会合并到“摇篮自述”中。现在,这里是相关的部分

附件 支架支持写入、读取和删除附件。读写操作可以是缓冲的,也可以是流式的

书写 您可以缓冲整个附件正文,并将其作为单个请求一次性发送。附件上载完成或发生错误后,将触发回调函数

语法

db.saveAttachment(idData, attachmentData, callbackFunction)
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)
db.getAttachment(documentID, attachmentName, callbackFunction)
var readStream = db.getAttachment(documentID, attachmentName, callbackFunction)
db.removeAttachment(documentID, attachmentName, callbackFunction)
示例 假设要将文本文档保存为名为“fooAttachment.txt”且内容为“Foo document text”的附件

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)
})
阅读 缓冲 您可以缓冲整个附件并一次接收所有附件。下载完成或出现错误后,回调函数将启动。回调中的第二个参数是附件的二进制数据

语法

db.saveAttachment(idData, attachmentData, callbackFunction)
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)
db.getAttachment(documentID, attachmentName, callbackFunction)
var readStream = db.getAttachment(documentID, attachmentName, callbackFunction)
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.getAttachment(id, attachmentName, function (err, reply) {
  if (err) {
    console.dir(err)
    return
  }
  console.dir(reply)
})
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)
})
示例 假设您想读回名为“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)
示例 假设您要删除以“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)
})
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)
})
var doc=
变量id=单据编号
var attachmentName='foo.txt'
db.removeAttachment