[英]Upload file on the server through REST API
我正在尝试通过REST API在服务器上上传文件。 我的api请求FormData模型,但是我收到400 bad request
错误。
我在devtools中的请求中的有效负载
------ WebKitFormBoundaryvPiRGxMC3Ru3AoFA内容处置:表格数据; name =“文件”; filename =“ test.jpg”内容类型:image / jpeg
------ WebKitFormBoundaryvPiRGxMC3Ru3AoFA--
我的文档中的有效载荷
文件:(二进制)
请看下面的一些代码。
upload.component.html
<input type="file" accept="image/*" (change)="onFileChange($event)">
upload.component.ts
uploadForm = this._fb.group({
profile: ['']
});
formData: FormData = new FormData();
...
onFileChange(event): void {
if (event.target.files && event.target.files.length) {
const file = event.target.files[0];
this.uploadForm.get('profile').setValue(file);
};
}
...
upload() {
this.formData.append('file', this.uploadForm.get('profile').value);
// API REQUEST
}
`
这是将输入值更改为FormData
的好方法吗?
您能从以下位置更改您的代码吗
this.formData.append('file', this.uploadForm.get('profile'.value);
至
this.formData.append('file', this.uploadForm.get('profile').value);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.