如何使用angularjs在html中显示二进制图像?

时间:2015-07-05 13:59:56

标签: javascript angularjs node.js express gridfs

我正在试图以angularjs html形式显示二进制数据图像。我正在打算两个回复。我不知道如何避免我已经附上我的screetshot.1st一个是我的回复它通过不良反应请一些人帮助我.i已附上客户端和服务器端控制器

client side controller
'use strict';

/**
 * @ngdoc object
 * @name test1.Controllers.Test1Controller
 * @description Test1Controller
 * @requires ng.$scope
*/
angular
.module('test1')
    .controller('Test1Controller', [
       '$scope','$http' ,'$location','$window',
        function($scope,$http, $location,$window)
		{
	$scope.image = {};
var image="download.jpg";
			$http.get('*/upload/'+image).success(function(data,status,response)
{ 
		console.log(data);
		$scope.image=data;
	var testJpg = $scope.image;
document.getElementById("myimage").src = testJpg;
		}); 

    }
]);

backend controller

'use strict';
 
 
var mongoose = require('mongoose'),
    _ = require('lodash');
 
var Grid = require('gridfs-stream');
Grid.mongo = mongoose.mongo;
var gfs = new Grid(mongoose.connection.db);
 
exports.create = function(req, res) {
 
                console.log(req.files.filefield);
            var part = req.files.filefield;
 
                var writeStream = gfs.createWriteStream({
                    filename: part.name,
                    mode: 'w',
                    content_type:part.mimetype
                });
 
 
                writeStream.on('close', function() {
                     return res.status(200).send({
                        message: 'Success'
                    });
                });
                
                writeStream.write(part.data);
 
                writeStream.end();
 
};
 
 
exports.read = function(req, res) {
 
    gfs.files.find({ filename: req.params.filename }).toArray(function (err, files) {
 
        if(files.length===0){
            return res.status(400).send({
                message: 'File not found'
            });
        }
    
       res.writeHead(200, {'Content-Type': files[0].contentType});
        
        var readstream = gfs.createReadStream({
              filename: files[0].filename
        });
        

        var bufs=[];
        readstream.on('data', function(data) {
           // res.write(data);
           bufs.push(data);
        }).on('end', function() {
           // res.end();
           var fbuf = Buffer.concat(bufs);
           var base64 = (fbuf.toString('base64')); 
           //console.log(base64 );
           res.end('"data:image/jpeg;base64,' + base64 + '";');  

        });
 
        readstream.on('error', function (err) {
          console.log('An error occurred!', err);
          throw err;
        });
    });
 
};
<div ng-controller="Test1Controller" >
<img ng-src="" id="myimage" />
</div>

0 个答案:

没有答案