دریافت و نمایش تصاویر از سرور در برنامههای Angular
نویسنده: وحید نصیری
تاریخ: ۱۳۹۶/۱۰/۳۰ ۱۱:۵۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using Microsoft.AspNetCore.Mvc;
namespace AngularTemplateDrivenFormsLab.Controllers
{
[Route("api/[controller]")]
public class ShowImagesController : Controller
{
[HttpGet("[action]")]
public IActionResult GetImage()
{
return File("~/assets/resume.png", "image/png");
}
}
} import { Injectable } from "@angular/core";
import { Observable } from "rxjs/Observable";
import { HttpClient } from "@angular/common/http";
@Injectable()
export class DownloadBinaryDataService {
constructor(private httpClient: HttpClient) { }
public getImage(): Observable<Blob> {
return this.httpClient.get("/api/ShowImages/GetImage", { responseType: "blob" });
}
} import { Injectable } from "@angular/core";
function getWindow(): any {
return window;
}
@Injectable()
export class WindowRefService {
get nativeWindow(): any {
return getWindow();
}
} import { WindowRefService } from "./window.service";
@NgModule({
providers: [
WindowRefService
]
})
export class CoreModule {} const urlCreator = this.nativeWindow.URL; imageBlobUrl = urlCreator.createObjectURL(imageDataBlob);
<img src="blob:http://localhost:5000/9d4bae44-00bc-4ed8-9f27-cac2de5ecd5d">
img-src 'self' data: blob:
import { WindowRefService } from "./../../core/window.service";
import { DownloadBinaryDataService } from "app/angular-http-client-blob/download-binary-data.service";
import { Component, OnInit, ViewChild, ElementRef } from "@angular/core";
import { DomSanitizer, SafeUrl } from "@angular/platform-browser";
@Component({
templateUrl: "./download-blobs.component.html",
styleUrls: ["./download-blobs.component.css"]
})
export class DownloadBlobsComponent implements OnInit {
@ViewChild("sampleImage1") sampleImage1: ElementRef;
private nativeWindow: Window;
imageBlobUrl: string;
sanitizedImageBlobUrl: SafeUrl;
constructor(private downloadService: DownloadBinaryDataService,
private windowRefService: WindowRefService, private sanitizer: DomSanitizer) { }
ngOnInit() {
this.nativeWindow = this.windowRefService.nativeWindow;
this.downloadService.getImage().subscribe(imageDataBlob => {
const urlCreator = this.nativeWindow.URL;
this.imageBlobUrl = urlCreator.createObjectURL(imageDataBlob); // doesn't work -> <img [src]="imageBlobUrl">
this.sampleImage1.nativeElement.src = this.imageBlobUrl; // works -> <img #sampleImage1>
this.sanitizedImageBlobUrl = this.sanitizer.bypassSecurityTrustUrl(this.imageBlobUrl); // works -> <img [src]="sanitizedImageBlobUrl">
});
}
} <h1>Angular HttpClient Blob</h1> <h4>#sampleImage1</h4> <img #sampleImage1> <h4>imageBlobUrl</h4> <img [src]="imageBlobUrl"> <h4>sanitizedImageBlobUrl</h4> <img [src]="sanitizedImageBlobUrl">
this.imageBlobUrl = urlCreator.createObjectURL(imageDataBlob); // doesn't work -> <img [src]="imageBlobUrl">
<h4>imageBlobUrl</h4> <img [src]="imageBlobUrl">
<img _ngcontent-c1="" src="unsafe:blob:http://localhost:5000/a4505339-5da2-4303-949c-8e6a7cfff2fc">
Refused to load the image 'unsafe:blob:http://localhost:5000/a4505339-5da2-4303-949c-8e6a7cfff2fc' because it violates the following Content Security Policy directive: "img-src 'self' data: blob:".
this.sanitizedImageBlobUrl = this.sanitizer.bypassSecurityTrustUrl(this.imageBlobUrl); // works -> <img [src]="sanitizedImageBlobUrl">
<img [src]="sanitizedImageBlobUrl">
<img _ngcontent-c1="" src="blob:http://localhost:5000/a4505339-5da2-4303-949c-8e6a7cfff2fc">
<img #sampleImage1>
@ViewChild("sampleImage1") sampleImage1: ElementRef; this.sampleImage1.nativeElement.src = this.imageBlobUrl; // works -> <img #sampleImage1>
<ng-component _nghost-c1=""><h1 _ngcontent-c1="">Angular HttpClient Blob</h1> <h4 _ngcontent-c1="">#sampleImage1</h4> <img _ngcontent-c1="" src="blob:http://localhost:5000/a4505339-5da2-4303-949c-8e6a7cfff2fc"> <h4 _ngcontent-c1="">imageBlobUrl</h4> <img _ngcontent-c1="" src="unsafe:blob:http://localhost:5000/a4505339-5da2-4303-949c-8e6a7cfff2fc"> <h4 _ngcontent-c1="">sanitizedImageBlobUrl</h4> <img _ngcontent-c1="" src="blob:http://localhost:5000/a4505339-5da2-4303-949c-8e6a7cfff2fc"> </ng-component>
export interface Photo {
id: number;
name: string;
size?: number;
path: string;
} src="unsafe:G:\UploadCenter\Media\Images\6ddbf898-9bb4-47eb-b728-29a5465e738c.jpg"