Javascript 从nodejs中的url读取图像
我想从url读取图像并将其存储在mongo db中。我基本上已经读取了字符串值并完成了上述过程。但我一直在想如何读取图像。关于这方面的任何想法都会非常有用。使用节点0.8.8和进行了测试Javascript 从nodejs中的url读取图像,javascript,node.js,Javascript,Node.js,我想从url读取图像并将其存储在mongo db中。我基本上已经读取了字符串值并完成了上述过程。但我一直在想如何读取图像。关于这方面的任何想法都会非常有用。使用节点0.8.8和进行了测试 使用节点0.8.8和进行测试 您需要下载或存储图像的帮助吗,或者两者都可以?只需请求下载img并写入文件系统例如,如果我有一个url作为localhost:1340/promotionDetails?promotion\u id=PROM008765,我可以使用express模块作为req.id读取promot
使用节点0.8.8和进行测试
您需要下载或存储图像的帮助吗,或者两者都可以?只需请求下载img并写入文件系统例如,如果我有一个url作为localhost:1340/promotionDetails?promotion\u id=PROM008765,我可以使用express模块作为req.id读取promotion\u id。我可以对图像执行同样的操作吗?您需要下载图像或存储图像的帮助吗,或者两者都可以?只需请求下载img并写入文件系统,例如,如果我有一个url作为localhost:1340/promotionDetails?promotion\u id=PROM008765,我就可以使用express模块作为req.id读取promotion\u id。我可以对图像执行同样的操作吗?
var http = require("http");
var mjs = require("mongojs");
// url of the image to save to mongo
var image_url = "http://i.imgur.com/5ToTZky.jpg";
var save_to_db = function(type, image) {
// connect to database and use the "test" collection
var db = mjs.connect("mongodb://localhost:27017/database", ["test"]);
// insert object into collection
db.test.insert({ type: type, image: image }, function() {
db.close();
});
};
http.get(image_url, function(res) {
var buffers = [];
var length = 0;
res.on("data", function(chunk) {
// store each block of data
length += chunk.length;
buffers.push(chunk);
});
res.on("end", function() {
// combine the binary data into single buffer
var image = Buffer.concat(buffers);
// determine the type of the image
// with image/jpeg being the default
var type = 'image/jpeg';
if (res.headers['content-type'] !== undefined)
type = res.headers['content-type'];
save_to_db(type, image);
});
});