首页 > 解决方案 > 如何将图像(角度 6)发布到 Web Api 2?

问题描述

我需要从 Angular 6 网站将图像(徽标)发送到 webApi 。实际上我的端点被角度客户端“触及”了。所以

从角度我想发送图像的二进制文件并将其接收到 web api 控制器中。我怎样才能做到这一点?(我真的不太擅长管理图像......)是二进制文件(角度的 File 类),webApi 中的 byte[] 是否相同?

谢谢你。

标签: arraysangularimagebinaryasp.net-web-api2

解决方案


您可以通过以下方式进行

html代码

  <input type="file" (change)="onFileChanged($event)" #fileInput>
  <button (click)="fileInput.click()">Select File</button>
  <button (click)="onUpload()">Upload!</button>

TS代码

import { HttpClient } from '@angular/common/http';

export class MyFileUploadComponent {
  selectedFile: File;

  constructor(private http: HttpClient) { }

  onFileChanged(event) {
    this.selectedFile = event.target.files[0]
  }

  onUpload() {

    // make sure you have injected HttpClientModule in app.module.ts

    this.http.post('my-backend.com/file-upload', this.selectedFile)
    .subscribe(... something here..);
  }
}

推荐阅读