Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.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
Javascript 如何使用GridFS在客户端返回大型base64映像?_Javascript_Angularjs_Node.js_Mongodb_Gridfs Stream - Fatal编程技术网

Javascript 如何使用GridFS在客户端返回大型base64映像?

Javascript 如何使用GridFS在客户端返回大型base64映像?,javascript,angularjs,node.js,mongodb,gridfs-stream,Javascript,Angularjs,Node.js,Mongodb,Gridfs Stream,我在MongoDB上使用GridFS存储图像,并希望在客户端显示这些图像。该代码适用于小图像,但当我想显示大于5个月的图像时,我在客户端没有返回 ResultController.js: images.forEach(function(item) { GridFileService.getBuffer(item.id, function(res) { var blob = UtilsService.b64toBlob(res, item.mimetype); //Con

我在MongoDB上使用GridFS存储图像,并希望在客户端显示这些图像。该代码适用于小图像,但当我想显示大于5个月的图像时,我在客户端没有返回

ResultController.js:

images.forEach(function(item) {
   GridFileService.getBuffer(item.id, function(res)
   {
       var blob = UtilsService.b64toBlob(res, item.mimetype); //Convert base64 to blob
       var blobURL = URL.createObjectURL(blob);
       $scope.resultsModel.originalImage.push(blobURL); //Store for display in image src tag
   });
});
GridFileService.js:

gridFileService.getBuffer = function(id, callback)
{
   $http(
   {
      method: 'GET',
      url: '/api/file_buffer/' + id
   }).then(function successCallback(response)
   {
      callback(response.data);
   }, function errorCallback(response)
   {
      AlertService.addAlert('danger', response.data);
   });
};
api.js:

app.get('/api/file_buffer/:id', routesFiles.getBufferFile);
routeFiles.js:

function getBufferFile(req, res, next)
{
    var idFile = req.params.id;

    //Get buffer from GridFS with id file
    gfs.readFile({_id: idFile}, function (err, data) {
        if (err)
        {
            log.error('Error on get buffer file ', err);
            return res.status(500).send(err.message);
        }
        else
        {
            //Convert buffer in base64
            var base64 = new Buffer(data).toString('base64');

            return res.status(200).send(base64); // return to client side with datas
        }
    });
}

如何快速返回大图像的缓冲区?

因为它是一个流。您应该在响应中管道化流。你为什么要尝试base64编码?为什么你有一个角度服务来阅读这个?只需动态更改图像标记上的
“src”
,并让浏览器完成工作。不要试图重新发明轮子,我不明白。我正在尝试base64编码,以便在blob中进行修改,从而显示图像。src标记已随URL对象动态更改。我有一个角度服务来提供图像的缓冲区,因为我有id文件或文件名,但没有存储在GridFS中的文件的缓冲区。我不知道如何以其他方式获得图像缓冲区。我知道你不明白。但是试着去做。当您可以更改
src
tage并将其下载时,为什么要尝试向服务发送base64编码的图像?和浏览器上的“缓存”!太棒了!关键是,你们根本并没有做你们想做的事情。我们只使用base64来处理一些愚蠢的小事情,比如这个网站导航栏上的所有图标。让图像正常加载。你只是在妨碍正常的过程。二进制流。img标签。它起作用了。