【问题标题】:Displaying images on angular app from node server从节点服务器在角度应用程序上显示图像
【发布时间】:2018-12-05 09:31:07
【问题描述】:

我在 localhost:3000 上运行了一个 nodejs 应用程序,我使用 multer 上传了图像,所以它们位于 ./uploads/ 文件夹中。 在 locahost:4200 上运行的 Angular 应用程序中,我想检索这些图像。

在我的 for 循环中:(projectImages 是每个项目对象的图像数组)

<div *ngFor="let i of project.projectImages">
   <img [src]=i.path alt="" > 
</div>

问题是路径显示为:localhost:4200/uploads/image.png 而不是 localhost:3000/uploads/image.png

更新: 通过向组件添加一个变量来解决这个问题,我现在得到:

WARNING: sanitizing unsafe URL value 

任何帮助将不胜感激!

更新2: 这是我的组件:

    import { Component, OnInit } from '@angular/core';
import { ProjectService } from '../services/project.service';
import { Router, ActivatedRoute } from '@angular/router';
import {Location} from '@angular/common';
import { DomSanitizer } from '@angular/platform-browser';


@Component({
  selector: 'app-project-details',
  templateUrl: './project-details.component.html',
  styleUrls: ['./project-details.component.css']
})
export class ProjectDetailsComponent implements OnInit {
  project:any;
  apiUrl :string = "localhost:3000";
  project_id:string;

  constructor(
    private router:Router,
    private activatedRoute: ActivatedRoute,
    private projectService:ProjectService,
    private _location: Location,
    private sanitization: DomSanitizer
  ) {
    this.activatedRoute.params
    .subscribe( params => {
      console.log(params.project_id)
      this.project_id = params.project_id;
      this.getProjectByID(params.project_id);
    })
   }

  ngOnInit() {
  }

getProjectByID(project_id:string){
  this.projectService.getProjectById(project_id).subscribe((data:any) => {
    this.project = data.project;
    this.project.projectImages.map(image => {
      image.path = this.sanitization.bypassSecurityTrustUrl(`${this.apiUrl}/${image.path}`.replace(/\\/g,"/"));
      console.log(image);
      return image;
});
    console.log(this.project.projectImages); 
  } , err => {
    console.log(err);
  });
}
}

注意:project.projectImages 是一个包含图像的数组,如下所示:https://ibb.co/jEgszo

【问题讨论】:

  • 您使用的是相对路径。如果您想使用 3000 而不是 4200,请给出 [src] 的完整路径。 [src]='localhost:3000\'+i.path 或者您可以将地址 localhost:3000 存储在 ts 文件中的变量中并在 html 中使用该变量
  • 我已经试过了,它说“报价不支持评估!”
  • 不,这不是错字,i.path 来自 for 循环,需要与 "localhost:3000\" 连接 .. 我在这上面花了太多时间
  • 所以我声明了一个变量并将 localhost:3000 设置为它,现在我收到 WARNING: sanitizing unsafe URL value
  • 使用[src]='server+i.path'。在.ts中设置server = "localhost:3000/"

标签: node.js angular multer


【解决方案1】:

使用完整路径并在使用前清理 url

import { DomSanitizer } from '@angular/platform-browser';
import { environment } from '../../../../environments/environment';

export class YourClass{
  project;
  apiUrl;

  constructor(private sanitization: DomSanitizer) {
      this.apiUrl = environment.apiUrl;
      project.projectImages.map(image => {
        image.path = this.sanitization.bypassSecurityTrustUrl(`${this.apiUrl}/${image.path}`);
        return image;
  });
  }
}

然后在你的html中你可以这样做

<div *ngFor="let i of project.projectImages">
   <img [src]=i.path alt="" > 
</div>

您的完整代码将如下所示

import { Component, OnInit } from '@angular/core';
import { ProjectService } from '../services/project.service';
import { Router, ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';
import { DomSanitizer } from '@angular/platform-browser';
import { environment } from '../../../../environments/environment';

@Component({
    selector: 'app-project-details',
    templateUrl: './project-details.component.html',
    styleUrls: ['./project-details.component.css']
})
export class ProjectDetailsComponent implements OnInit {
    project: any;
    apiUrl: string;
    project_id: string;
constructor(
    private router: Router,
    private activatedRoute: ActivatedRoute,
    private projectService: ProjectService,
    private _location: Location,
    private sanitization: DomSanitizer
) {
    this.apiUrl = environment.apiUrl;

    this.activatedRoute.params
        .subscribe(params => {
            console.log(params.project_id)
            this.project_id = params.project_id;
            this.getProjectByID(params.project_id);
        })
}

ngOnInit() {
}

getProjectByID(project_id: string) {
    this.projectService.getProjectById(project_id).subscribe((data: any) => {

        data.project.projectImages.map(image => {
            image.path = this.sanitization.bypassSecurityTrustUrl(`${this.apiUrl}/${image.path}`);
            return image;
        });
        this.project = data.project;
    }, err => {
        console.log(err);
    });
}

}

查看文档here

【讨论】:

  • 在您的组件中。你也可以编辑和添加你的组件吗?
  • 它与您使用的类相同,只是多了一个 DI 和对 projectImage 数组的一些修改
  • @SachinGupta 我添加了我的组件,你能看到吗?
  • 分配给this.project后复制@Obed给出的代码。
  • 我设法添加了 DomSanitizer 并且请求 URL 是正确的,当我将其复制粘贴到浏览器中时,图像会显示,但在 标签中显示:ibb.co/ioBHFT
【解决方案2】:

我猜你在你的节点应用程序中使用 express,你需要包含一个到资源/上传目录的静态路由(快速静态路由),如下所示:

app.use(express.static('resources/uploads'))

【讨论】:

    猜你喜欢
    • 2020-04-21
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多