简体   繁体   English

无法使用MEAN Stack显示图像

[英]Can't Display Image Using MEAN Stack

I am using the MEAN stack and trying to display an image stored in my Mongo database. 我正在使用MEAN堆栈,并试图显示存储在Mongo数据库中的图像。 I believe that the image is correctly stored and being correctly passed to the browser as when I use Web Console and look at the response to my Post the Response Body displays the image. 我相信该图像已正确存储,并已正确地传递给浏览器,就像我使用Web Console并查看对我的响应的响应一样。响应正文显示该图像。

I have a table and when a row is clicked it makes a call to my controller: 我有一张桌子,当单击一行时,它会调用我的控制器:

$scope.detailRequest = function(index){
    $scope.selectedRow = index;
    $scope.selected = this.contact;
        $http.post('/DetailRequest', $scope.selected).success(function(response){
            $scope.detailResponse = response;
        });
};

In Node I query Mongo and return the image: 在Node中,我查询Mongo并返回图像:

router.post('/DetailRequest', function(request, response) {
  reports
    .findOne({_id : request.body._id})
    .select('img')
    .exec(function (err, research) {
       response.contentType(research.img.contentType);
       response.send(research);   
    });
});

I am using the ng-src for image display. 我正在使用ng-src进行图像显示。 As I mention at the top of the page I can see the image correctly in the Response Body of the Web Console. 正如我在页面顶部提到的那样,我可以在Web控制台的响应正文中正确看到该图像。 Thus, I am confident I am getting the image back to the browser. 因此,我有信心将图像返回到浏览器。

<img ng-src="{{detailResponse}}">

The image just won't display 图片不会显示

You've got your wires crossed a bit here. 您的电线在这里有点交叉。

ng-src will set the Img src property to a URL. ng-src会将Img src属性设置为URL。 The browser will then load the src URL with a GET request. 然后,浏览器将使用GET请求加载src URL。

<img ng-src="{{someUrlRequestedWithGet}}">

So your Express route should be a GET : 因此,您的Express路线应为GET

router.get('/DetailRequest/:id', function(request, response) {
   reports
        .findOne({_id : request.params._id})
        .select('img')
        .exec( function  (err, research) {
          response.contentType(research.img.contentType);
          response.send(research);   
   });

Note: instead of putting the id in the body, you put it in the URL. 注意:您无需将ID放在正文中,而是将其放在URL中。

Hint: Whatever URL is in ng-src should display when pasted into a browser bar. 提示:粘贴到浏览器栏中时,应显示ng-src中的所有URL。

There's also a way to get the image data from a POST (although that's not really the best use of POST ). 还有一种方法可以从POST获取图像数据(尽管这并不是POST的最佳用法 )。 But you'll have to get a bit more tactical and use a data-uri 但是您将需要更多的战术,并使用data-uri

So something on these lines in Angular should work: JavaScript: 因此,Angular中这些行上的内容应该可以正常工作:JavaScript:
$scope.imageData = 'data:image/jpeg;base64' + Base64LongLongString;

Template: 模板:
<img ng-src="imageData" />

Let's see why it should work. 让我们看看为什么它应该起作用。
<img src=""> expects a uri, so that wouldn't work in your case if we feed uri in this attribute. <img src="">需要一个uri,因此如果我们在此属性中提供uri,则在您的情况下将不起作用。 ng-src with uri would have same problem. 带有uri的ng-src会有同样的问题。

An image with binary data embedded has the src attribute as: 嵌入了二进制数据的图像的src属性为:
<img src="data:image/jpeg;base64,/9j/Base64LongLongString">
More about this representation at http://ben.lobaugh.net/blog/33713/using-binary-image-data-to-display-an-image-in-html 有关此表示形式的更多信息,请访问http://ben.lobaugh.net/blog/33713/using-binary-image-data-to-display-an-image-in-html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM