angular - 如何获取图像并在我的页面中显示 angular 6

标签 angular typescript asp.net-web-api file-upload angular6

我正在将 uploader 中的图片保存到数据库中,所以它对我来说非常完美,但问题是如果我想在我的网站上显示该图片,那么我该如何显示它们,而且图片路径应该显示为 src ="http://www.example.com/image.png"。 请给我任何示例/解决方案。

TS

imageUrl: string = "/assets/img/default-image.png";
  fileToUpload: File = null;
  constructor(private imageService: ImageUploadService) { }

  handleInputFile(file: FileList){
    this.fileToUpload = file.item(0);
    var reader = new FileReader();
    reader.onload = (event:any) => {
      this.imageUrl = event.target.result;
    }
    reader.readAsDataURL(this.fileToUpload);
  }

  OnSubmit(Caption,Image){
    this.imageService.postFile(Caption.value, this.fileToUpload).subscribe(
      data => {
        console.log("Done");
        Caption.value = "";
        Image.value = "";
      }
    );
  }

服务.Ts

postFile(caption: string, fileToUpload: File){
    const endpoint = 'http://localhost:3343/api/UploadImage';
    const formData: FormData = new FormData();
    formData.append("Image", fileToUpload, fileToUpload.name);
    formData.append("Caption", caption);
    return this.http.post(endpoint,formData);
  }

HTML

<form #imageForm="ngForm" (ngSubmit)="OnSubmit(Caption,Image)">
        <div class="input-field col s12">
          <input type="text" #Caption ngModel name="Caption" id="Caption">
          <label for="Caption">Caption</label>
        </div>
        <img [src]="imageUrl" style="width:200px;height:200px;">
        <input type="file" #Image accept="Image/*" (change)="handleInputFile($event.target.files)">
        <button class="btn waves-effect waves-light" type="submit">Submit
          <i class="material-icons right">send</i>
        </button>
      </form>

最佳答案

我正在分享我的项目代码。在这个例子中,

我有一个 API,我用 customerNumber 调用它返回我blob类型数据。然后我将这些数据从服务返回到我的组件,在我的组件端,我将该数据作为 blob 并将其转换为 Image使用 this.createImageFromBlob 键入功能。最后,我使用 imageToShow.photo 在模板端显示该图像<img [src]="imageToShow.photo"> 中的变量标签。

我的 service.ts 端,

  getCustomerImages(customerNumber: number, type: string): Observable<File> {
    let result: Observable<any> = this.http
      .get(`${this.apiBaseUrl}csbins/Customer/${customerNumber}/images`, { responseType: "blob" });
    return result;
  }

我的 component.ts 端,

  getCustomerImages() {
    this.isImageLoading = true;
    this.getCustomerImagesSubscription = this.getCustomerService.getCustomerImages(this.refNo).subscribe(
      data => {
        this.createImageFromBlob(data);
        this.isImageLoading = false;
      },
      error => {
        this.isImageLoading = false;
      });
  }

并在您的组件中将 blob 转换为图像,

  createImageFromBlob(image: Blob) {
    let reader = new FileReader();
    reader.addEventListener("load",
      () => {
          this.imageToShow.photo = reader.result;
      },
      false);

    if (image) {
      if (image.type !== "application/pdf")
        reader.readAsDataURL(image);
    }
  }

和我的 template.html 端,

          <ng-template [ngIf]="imageTypeShow">
              <ng-template [ngIf]="imageToShow.photo">
                  <img [src]="imageToShow.photo" class="img-thumbnail" *ngIf="!isImageLoading;">
              </ng-template>
          </ng-template>

有什么不明白的地方欢迎提问。

关于angular - 如何获取图像并在我的页面中显示 angular 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55057774/

相关文章:

arrays - 如何在 TypeScript 中向对象数组添加具有不同值的新属性?

typescript - 使用 TypeScript 在 AngularJS 中实现提供程序

angular - ngrx:规范化结构与可观察实体

vb.net - Action 委托(delegate) c# 到 vb.net 的转换

c# - 为什么 WCF 服务作为 PID 4 (SYSTEM) 而不是托管它的进程的 PID 进行监听?

带单选按钮的 Angular 6 Material 数据表

javascript - 在 Angular 中解析来自异步函数的数据时出现空白图表

javascript - TS 如何在方法内使用类变量?

javascript - 在 JavaScript 中定义一个类?

c# - 如何在 Controller 级别使用路由