我想使用:
import { HttpClient } from '@angular/common/http'
... 为了达到如下效果:
curl -F "image=@blob.bin" someurl.com
不
this.http.post('http://someurl.com', fileContents)
如何使用Angular7和HttpClient服务获得这种效果?我没有与之交互的实际HTML表单,我只是需要发布一些规定的二进制/字符串内容 好像
<form method='POST' action='http://someurl.com' enctype='multipart/form-data'> <input type='file' name='update'> <input type='submit' value='Update'> </form>
FormData
const formData: FormData = new FormData(); formData.append('files', this.logo, this.logo.name); this.http.post("upload", formData)
在这个例子中, this.logo File 对象您可以从输入中获取文件对象,如下所示:
this.logo
File
<input (change)="handleLogoFileInput($event.target.files)" type="file" name="update />
handleLogoFileInput(files: FileList) { this.logo = files[0]; }