简体   繁体   English

HTML中未显示来自本地服务器的Angular 2图像网址

[英]Angular 2 Image Url from Local Server Not Displaying in Html

I have an Angular 2 Application that retrieves items from a local database. 我有一个Angular 2应用程序,可以从本地数据库中检索项目。 The server stores the image of the item and the database stores the path where it is stored on the server. 服务器存储项目的图像,数据库存储项目在服务器上的存储路径。 I can retrieve all the items without the problem but when it comes to displaying the picture it will not render because it says Unsafe Url . 我可以检索所有项目而不会出现问题,但是在显示图片时,由于显示Unsafe Url,因此无法渲染 ie 192.168.0.24:1025/UploadFile/freshmilk.jpg. 即192.168.0.24:1025/UploadFile/freshmilk.jpg。 So I Created a pipe to bypassSecurityTrustUrl. 所以我创建了一个绕过SecurityTrustUrl的管道。 After that, the error disappears but still does not display the image. 此后,错误消失,但仍不显示图像。

After inspecting the Html the image src says the correct Url for the image but when I hover my mouse it actually appended the localhost:5555 before the image Url . 检查Html后,图像src会说出图像的正确网址,但是当我将鼠标悬停时,它实际上在图像网址之前附加了localhost:5555 The problem is that the image will not display since localhost:5555 is appended to the image Url. 问题在于,由于将localhost:5555附加到了图像URL,所以该图像将不会显示。 How do I remove this appended localhost:5555 in the image Url? 如何在图片网址中删除此附加的localhost:5555?

[Localhost:5555 appended with the image Url][1] [Localhost:5555附加了图像网址] [1]

I tried hard coding one of the images from the server without using data binding. 我尝试不使用数据绑定对服务器中的图像之一进行硬编码。 Which resulted in the Green Milk image next to the cat while the Cat is an image Url from the Internet. 这导致猫旁边有绿色牛奶图像,而猫是来自互联网的图像网址。

This is the Html Part of my Code 这是我代码的HTML部分

<img src="http://192.168.0.24:1025/UploadFile/freshmilk.jpg" width="100" height="100">
<img [src]="picturetest | safeHtml" width="100" height="100">

<div class="table">
    <table class="table">
        <thead>
        <th>Picture</th>
    <th>ID</th>
    <th>Name</th>
    <th>Quantity Stored</th>
    <th>Price</th>
    <th>Purchase Count</th>
    <th>Options</th>

    </thead>`enter code here`
    <tbody>
        <tr *ngFor="let item of items.data">
            <td><img [src]="item.picture | safeHtml" width="100" height="100"></td>
            <td>{{item.itemID}}</td>
            <td>{{item.itemName}}</td>
            <td>{{item.itemQuantityStored}}</td>
            <td>{{item.itemPrice}}</td>
            <td>{{item.purchaseCount}}</td>           
        </tr>


    </tbody>
</table>

I also tried removing the Pipe and manually looping the bypass security 我还尝试了删除管道并手动循环绕过安全性

   this.Data.getProducts().subscribe(data => {
          this.items.data=data
          console.log(this.items.data);
          for(var i=0;i<this.items.data.length;i++)
           this.items.data[i].picture=this.sanitizer.bypassSecurityTrustUrl(this.items.data[i].picture);
            console.log("latestest");

    });

I think that you do not need the interpolation in your binding. 我认为您不需要在绑定中进行插值。 As you can see at this plunker simple assignment, such as 如您所见,这个简单的任务如

src={{path}}

works well for external file sources. 适用于外部文件源。

You can even see that you probably do not have to do something that hardcore as bypassing security ... you could have sanitized the data, but even that is overkill per my opinion in this case. 您甚至可以看到您可能不必绕过安全性而做一些刻板的事情……您可以对数据进行清理,但是根据我的观点,在这种情况下,这还是过头了。

I think that the problem is in the way you store and retrieve your data. 我认为问题出在您存储和检索数据的方式上。 I would recommend you moving to some flexible backend such as Firebase and using full capabilities of those tools, at least until you will not be able to create and maintain your own hosting/api/db properly. 我建议您使用一些灵活的后端,例如Firebase,并充分利用这些工具的全部功能,至少直到无法正确创建和维护自己的托管/ api / db为止。 I totally do not like resources path in a form 666.66.0.6:4200/my/secret/path/little_pink_kitten.jpg ... Per your localhost appearing in the path, try prefix the src url with https:// or http:// alternatively ... you should be able to store all your data in simple key-value database structure accompanied with some kind of api allowing you use ajax/promises/etc in your queries, not just directly ask the server with some absolute path. 我完全不喜欢666.66.0.6:4200/my/secret/path/little_pink_kitten.jpg格式的资源路径...对于出现在路径中的本地主机,请尝试在src url前面加上https://或http:/ /或者...您应该能够将所有数据存储在简单的键值数据库结构中,并附带某种api,允许您在查询中使用ajax / promises / etc,而不仅是直接向服务器询问绝对路径。

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

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