2016-08-19 35 views
2

我想发送.jog图像到客户端,但我失败了。如何提供图像到客户端nodejs和角

app.get('/image', function(req, res){; 


res.sendfile(path.resolve(path.resolve(__dirname,'/Imagepath/image.jpg"))); 

});

在我的角侧

$http({ 
    method:'GET', 
    url:'/image', 
    }).then(function (response){ 
    $scope.image= response.data; 


    }) 


<img id="imgid" ng-src="{{image}}" width="500" height="400"/> 

的问题是,该响应是一些二进制数据。我怎样才能让它发送可以显示在img src上的数据。如何在客户端的服务器文件夹中显示图像?谢谢。

回答

0

理解,<img>标签不通过属性支持二进制 采购,所以你必须看看另一种解决方案。

你可以尝试转换二进制编码为Base64在客户端 使用 TypedArrays 与 ​​ 共同发挥作用。然后你可以使用

<img ng-src="data:image/JPEG;base64,{{myImage}}"> 

回答How to return image from $http.get in AngularJS

所以基本上你需要采取response.data并转换为Base64,然后将其分配给$scope.image。之后,您可以使用上面提到的img标签来显示它。

1

除非需要将图像作为数据发送(例如动态生成),否则您可以将图像的路径设置为src。

假设你正在使用express.js然后你可以它来提供你的图像作为静态内容:http://expressjs.com/en/starter/static-files.html

app.use(express.static('public')); 

,并与存储在public/images/image.jpg您的角码的图像可以只加载图像:

$scope.image = "/images/image.jpg" 

<img id="imgid" ng-src="{{image}}" width="500" height="400"/>