我正在将使用输入标记选择的图像上载到api服务器响应- {status: false, message: "Could not upload"},component.html -
<input type="file" (change)="fileChange($event)" id="upload" style="display:none" accept="image/*" capture="environment">Component.ts
fileChange(event): void {
const fileList: FileList = event.target.files;
if (fileList.length > 0) {
const file = fileList[0];
const formData = new FormData();
formData.append('file', file, file.name);
const headers = new Headers();
this.http.post(SERVER_URL, formData).subscribe(
res => {
console.log(res)
}
)
}
}我正在使用Interceptor进行身份验证。其他所有的apis都能正常工作,我不知道为什么它不能工作。
发布于 2019-11-04 19:38:26
尝试添加该方法,并在不使用文件名的情况下进行测试,如下所示:
this.formData.append('_method', 'POST');
this.formData.append('file', file);对我来说很管用。希望它能帮上忙!
https://stackoverflow.com/questions/58692378
复制相似问题