如何在 html 代码中引用图像,这是一个文档 couchdb

How to reference images within html code which is a document couchdb

我正在开发基于 PhoneGap Build 的离线应用程序(单页应用程序)。要更新某些部分的某些内容,我会使用从 CouchDB 服务器到应用程序上的 PouchDB 的复制。我将这些部分视为包含 html 代码和必要图像作为附件的 CouchDB 文档。接下来的问题是:如何使 HTML 中的图像路径与 CouchDB 中用于附件的图像路径保持对齐?

保持 DOM 图像与 PouchDB 附件同步的最简单方法可能是将附件作为 Blob 提取。 blob-util 对于处理 blob 非常有帮助,但它通常很简单:

// fetch the document and attachments
db.get('doc', {
  attachments: true,
  binary: true
}).then(function (doc) {
  // get the blob from the document
  var blob = doc._attachments['attachment.png'].data;
  // convert the blob to a url
  var url = blobUtil.createObjectURL(blob);
  // set the url on the `<img>` tag
  img.src = url;
}).catch(console.log.bind(console));