Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
739 views
in Technique[技术] by (71.8m points)

node.js - get image from backend api and display in angular

I want display image in angularV10 and get it from backend and I don't know why image not display and got error I'm looking for how to solve but I don't get answer please can someone guide me

back-end:

     get_image:async(req,res,next)=>{
        Image.findAll().then(data=>{
          res.send(data)
        }) }

api:

    router.get("/get_image",uploadController.get_image)

Front-end Angular : service.ts

    get_file(): Observable<any>{
      return this.http.get(baseUrl + '/get_image'  , { responseType: 'Blob' as 'json' })}

code:

      createImageFromBlob(image: Blob) {
         let reader = new FileReader();
         reader.addEventListener("load", () => {
            this.imageToShow = reader.result; <<< this.imageToShow
         }, false);
      
         if (image) {
            reader.readAsDataURL(image);
            console.log(image)
         }
      }
    
      get_image():void{
    this.AddFileService.get_file().subscribe(data=>{
      this.createImageFromBlob(data);
    console.log(data)
    })}

html:

    <img [src]="imageToShow "/>

Error :

big error

unsafe:data:application/json;base64, ..... alot of chars i don't under stand 

here table of image in mysql

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Not much in the way of detail to this, but hopefully I can at least clear some things up for you so that you can find your footing with the issue a little better.

The lot of characters you don't understand are the base64 encoded string of the image (if your code is producing an image, appropriately, at least).

What you want to show as an image is a data URI and it looks much like you've shown:

data:image/jpeg;base64,[a lot of characters]

Depending on the actual image type, it might not be image/jpeg, it might be image/png, etc.

There's two things wrong with that final block you've shown:

unsafe:data:application/json;base64, ..... alot of chars i don't under stand 

The first one, having now told you what it should look like, is that it thinks the data is application/json instead of the expected image/xyz. So your process for constructing that data URI is wrong somewhere. I suspect it's where you are telling in your blob type is supposed to be json (thus, application/json):

get_file(): Observable<any>{
  return this.http.get(baseUrl + '/get_image'  , { responseType: 'Blob' as 'json' })}

The second is the clue to the main issue that you are really seeing: unsafe:....

In order to display images in Angular in this fashion, you need to put those URIs and whatnot through the DomSanitizer:

constructor(private readonly sanitizer: DomSanitizer) {}

public safeImage: SafeUrl;

private getImage(...): void {
  const imageBase64String = this.createImageFromBlobOrSomething(...);

  this.safeImage = this.sanitizer.bypassSecurityTrustUrl(imageBase64String);
}

And your template will then use safeImage instead:

<img [src]="safeImage"/>

That will stop the unsafe:... error, but then you'll find that you won't see an image, because the data URI is for application/json, instead of an image type, which you'll need to go ahead and fix.

Edit: My approach for multiple images is to save the images (if you want to keep the initial images for further usage/manipulation) or just only save the safe url version of them...

this.rawImages = data; // Wherever your images come from, and if you want to keep them...

this.safeImages = [];
this.rawImages.forEach((img) => {
  this.safeImages.push(this.sanitizer.bypassSecurityTrustUrl(img));
});

Then instead of *ngForing the raw images themselves, do it over the safeImages array instead:

<div *ngFor="let safeUrl of safeImages">
  <img [src]="safeUrl">
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...