【问题标题】:How to post (in angular 6) an image to a Web Api 2?如何将图像(角度 6)发布到 Web Api 2?
【发布时间】:2018-11-27 06:48:06
【问题描述】:

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

我想从角度发送图像的二进制文件并将其接收到 Web api 控制器中。我怎样才能做到这一点? (我真的不好经营形象……) 是二进制文件(角度中的 File 类),与 webApi 中的 byte[] 相同吗?

谢谢。

【问题讨论】:

  • 请贴一些你试过的代码
  • 它只是字节[]。您可以像发送任何类型的文件一样发布它。

标签: arrays angular image binary asp.net-web-api2


【解决方案1】:

您可以通过以下方式进行操作

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..);
  }
}

【讨论】:

  • 谢谢乔尔,我知道...我已经在其他问题中阅读过它,但我的问题是如何从文件类型(在 Angular 中)中获得一个字节 [] 在我的控制器中....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-27
  • 2019-08-14
  • 1970-01-01
  • 2018-10-12
  • 1970-01-01
  • 1970-01-01
  • 2019-06-26
相关资源
最近更新 更多