arrays - 如何将图像(角度 6)发布到 Web Api 2?
问题描述
我需要从 Angular 6 网站将图像(徽标)发送到 webApi 。实际上我的端点被角度客户端“触及”了。所以
从角度我想发送图像的二进制文件并将其接收到 web api 控制器中。我怎样才能做到这一点?(我真的不太擅长管理图像......)是二进制文件(角度的 File 类),webApi 中的 byte[] 是否相同?
谢谢你。
解决方案
您可以通过以下方式进行
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..);
}
}
推荐阅读
- python - Spyder 探查器时间不加起来(totalTime =/= localTime + 子函数 totalTime)
- python - Google Colab:运行 fig.write_image() 的问题
- c# - 从服务器物理地址获取虚拟机 IP 地址
- android - 使用 cameraX 1.0.0-beta11 录制视频不起作用
- python - 无法将矩阵附加到python中的数组
- python - Python:防止在关闭时删除临时文件
- python - 使用网络抓取在 python 中使用正则表达式排除部分字符串
- python - 有没有办法调整 numbas 编译以停止使用时间进行 JIT 编译?
- android - 如何创建没有上下文属性的自定义 arrayAdapter
- javascript - Ajax 每秒设置超时