- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在 GridFS 中上传图像,但不知道如何在 <img >
中显示它标签。
我尝试了以下代码:
conn.once('open', function () {
var gfs = Grid(conn.db, mongoose.mongo);
gfs.files.find({ filename: 'img1.png' }).toArray(function (err, files) {
if (err) { console.log(err); }
console.log(files);
});
});
我得到结果:
[ { _id: 5316f8b3840ccc8c2600003c,
filename: 'img1.png',
contentType: 'binary/octet-stream',
length: 153017,
chunkSize: 262144,
uploadDate: Wed Mar 05 2014 15:43:07 GMT+0530 (India Standard Time),
aliases: null,
metadata: null,
md5: '915c5e41a6d0a410128b3c047470e9e3' } ]
现在这只是文件信息而不是实际的文件数据。
如何在 HTML 中显示图像?
最佳答案
您必须定义响应的内容类型,并使用数据库连接来获取数据。检查以下示例以获得一个想法:
app.get('/filelist', function(req, res) {
var collection = db.collection('DbCollectionName');
var da = collection.find().toArray(function(err, items) {
console.log(items[0]);
res.writeHead(200, {'Content-Type': 'image/png'});
res.end(items[1].dbfileName.buffer, 'binary');
});
});
编辑:
因此,当您要将图像设置为源时,可以将图像的二进制数据(从数据库获取)转换为base64格式。
var img = document.createElement('img');
img.src = 'data:image/jpeg;base64,' + btoa('your-binary-data'); //JS have btoa() function for it.
document.body.appendChild(img);
或者如果您的图像不支持上述内容,您也可以使用十六进制转base64
function hexToBase64(str) {
return btoa(String.fromCharCode.apply(null, str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
}
并将其命名为
img.src = 'data:image/jpeg;base64,' + hexToBase64('your-binary-data');
关于node.js - 显示来自 GridFS 的 HTML 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22196125/
如何通过 mongo shell 将文件“上传”到我的 MongoDB 以及如何检索同一文件?我想知道,这样做之后的输出是什么。 我认为展示我所做的将表明我的尝试是多么愚蠢。 db.fs.files.
我找到了 this benchmark但它很旧(2010)。 有没有人使用以下方法进行基准测试: 使用 Nginx-GridFS 提供图片 VS 图片来自Tornado 最佳答案 我们一直在使用我们的
我通过代码片段将文件写入gridfs,但我无法找到将字符串更新到Gridfs的写入方法。 下面的代码片段将使用路径进行更新,但是直接字符串缓冲区怎么样? var metadata = { "p
我想更新 GrifFs 文件集合中元数据的单个字段。 我阅读了有关 Spring Data MongoDB 的文档,但没有找到任何 API。 到目前为止,我找到的唯一解决方案是直接使用 Mongo A
使用gridfs-stream,查找和抓取文件时如何指定bucket名称? 我的问题是在 stackoverflow 上发现的以下问题的后续问题 How can I specify a GridFS
我正在开发 NodeJs API,我选择使用 GridFS 来存储和下载文件。对于这个问题,我有两个 API:上传和下载。当我使用 Postman 调用它们时,这两个 API 都工作得很好;但是我在使
我正在尝试在我的 node.js 应用程序中设置文件 API。我的目标是能够将文件流直接写入 gridfs,而无需最初将文件存储到磁盘。看来我的创建代码正在运行。我能够将文件上传保存到gridfs。问
我正在使用最新版本的express、mongodb、nodejs等。我没有使用主体解析中间件,因为我上传文件时不想将文件存储在磁盘上。我只想将它们直接流式传输到 GridFs 中。我正在使用 grid
在将元数据信息实际存储到 mongoDB 之前,我正在努力将其附加到 dfile。我正在使用: const express = require("express"); const router = e
如何将 1.000.000 个带有 css、js、img 文件的网页保存到 mongodb+gridfs 中?如何定义页面和html文件之间的链接? 最佳答案 一般的sql->mongodb看这里:
GridFS 是 MongoDB 的一种规范,用于存储和检索大型文件,如图像、音频、视频等。GridFS 也是一种存储文件的文件系统,但其数据是存储在 MongoDB 集合中的,GridFS 甚至可以
通常我通过从 MongoDb 加载数据来创建 XLSX 二进制对象。然后通过 http 响应将其传输到客户端。这是我使用的代码,效果非常好。 服务器端代码: //Row data (rows) gen
我是NoSQL的新手,我正在使用MongoDB作为支持数据库,在Grails框架中开发零件和 Material 数据库。我正在尝试自动进行项目的初始批量导入,并且我正在寻找一些有关从何开始的想法。 我
我在应用程序中添加了一个步骤,通过 GridFS 保存文件,并添加了一个名为“已处理”的元数据字段,作为计划任务的标志,该任务检索新文件并将其发送以进行处理。由于 GridFS 的 Java 驱动程序
我正在使用一个遗留应用程序,该应用程序的数据模型中给定的 MongoDB 文档的大小有时可能会超过 16MB。这当然会在 Mongo 中触发异常,因此我们决定将一些集合转移到没有此大小限制的 Grid
给定一个输入流、一个文件名字符串和一个路径字符串,我如何使用 Java 将文件存储在 GridFS 中?现在我在没有路径的情况下保存了它: public ObjectId saveFile(Input
考虑“餐厅”mongodb 数据库中的以下“餐厅”集合文档。 { "_id" : ObjectId("55f6564073fc0a09338cf434"), "address" : { "bu
我正在做一些测试,看看我们是否可以在 MongoDB 上使用 GridFS 来存储文件以供将来的应用程序使用;我正在使用 10gen 的 C# 驱动程序将一个 80Mb 的文件“上传”到数据库。 第一
在使用 mongo gem 的 ruby 中,我找不到任何关于如何使用 GridFs 查找文件名的文档。 最佳答案 第一个get a connection to the database ,我们称
我正在使用 node.js 和 mongodb 开发类似 facebook 的应用程序。我已经能够成功地将图像文件上传到 GridFS 并使用 node-mongodb-native 驱动程序 wik
我是一名优秀的程序员,十分优秀!