html Angular:下载并显示图像

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html Angular:下载并显示图像相关的知识,希望对你有一定的参考价值。

export interface IFileResponse {
    fileName?: string;
    fileDownloadUri?: string;
    fileType?: string;
    size?: number;
}

export class FileResponse implements IFileResponse {
    constructor(public fileName?: string, public fileDownloadUri?: string, public pubfileType?: string, public size?: number) {}
}
import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';

type EntityResponseType = HttpResponse<IFileResponse>;
type EntityArrayResponseType = HttpResponse<IFileResponse[]>;

@Injectable({
    providedIn: 'root'
})
export class FileService {
    public resourceUrl = SERVER_API_URL + 'api/files';

    constructor(protected http: HttpClient) {}

    upload(file: File): Observable<EntityResponseType> {
        const formData = new FormData();
        formData.append('file', file);

        return this.http.post<IFileResponse>(`${this.resourceUrl}/`, formData, { observe: 'response' });
    }

    download(fileName: string) {
        return this.http.get(`${this.resourceUrl}/${fileName}`, { observe: 'response', responseType: 'blob' });
    }
}
export interface ICourse {
    imageUrl?: string;
}

export class Course implements ICourse {
    constructor(
        public imageUrl?: string
    ) {}
}
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
import { FileService } from 'app/files/file.service';
import { IFileResponse } from 'app/shared/model/file-response.model';

@Component({
    selector: 'entity-update',
    templateUrl: './entity-update.component.html'
})
export class EntityUpdateComponent implements OnInit {
  imageMain?: File;
  
  constructor(
        //...
        protected fileService: FileService,
        protected fb: FormBuilder
    ) {}
    
    setNewForm(institutionId: number) {
        this.form = this.fb.group({
            imageUrl: new FormControl()
        });
    }
    
    onFileSelect(event) {
        if (event.target.files.length > 0) {
            const file = event.target.files[0];
            console.log('selected file: ' + file.name);
            this.imageMain = file;
        }
    }
    
    uploadMainImage(file: File) {
        console.log(`uploading main image: ` + file.name);
        this.fileService.upload(file).subscribe(
            (response: HttpResponse<IFileResponse>) => {
                this.course.imageUrl = response.body.fileName;
            },
            (res: HttpErrorResponse) => {
                console.error(`error saving file: ${file.name}`);
            }
        );
    }
}
<div class="form-group">
  <label class="form-control-label" for="imageMain">Image Main</label>
  <input type="file" formControlName="imageMain" name="imageMain" id="imageMain"
   (change)="onFileSelect($event)">
</div>
import { FileService } from 'app/files/file.service';
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';

@Component({
    selector: 'entity-detail',
    templateUrl: './entity-detail.component.html'
})
export class EntityDetailComponent implements OnInit {
    imageMain?: Blob;
    imageUrl?;
    
    constructor(
        protected fileService: FileService,
        protected sanitizer: DomSanitizer
    ) {}
    
    ngOnInit() {
        this.activatedRoute.data.subscribe(({ entity }) => {
            this.entity = entity;
            this.fileService.download(this.entity.imageUrl).subscribe(res => {
                console.log('image downloaded...');
                this.imageMain = new Blob([res.body], { type: res.headers.get('Context-Type') });
                this.imageUrl = this.sanitizer.bypassSecurityTrustResourceUrl(URL.createObjectURL(this.imageMain));
            });
        });
    }
}
 <!-- main image -->
<div>ImageMain: {{course.imageMain}}</div>
<img *ngIf="imageUrl" [src]="imageUrl"/>

以上是关于html Angular:下载并显示图像的主要内容,如果未能解决你的问题,请参考以下文章

Angular 5 如何下载远程图像

使用 Angular 7 从 Firebase 存储中检索图像

asp核心和angular7中没有“Access-Control-Allow-Origin”标头

用于从网站下载图像并显示文件大小的 Java 程序

无论如何通过将随机验证码文本转换为图像并在html中显示来使用角度创建验证码

在 Angular2 中显示默认图像