angular - 将文件从 Angular 上传到 Laravel。不工作
问题描述
我正在尝试将图像从 angular(前端)上传到 laravel(后端)。
角度项目
product.component.html
<form [formGroup]="mediaFormGroup" (ngSubmit)="onSubmit();">
<input type="file" name="thumb_nail" (change)="onFileChange($event,'thumbnail')" />
<button type="submit" >UPLOAD</button>
</form>
product.component.ts
thumbFile: File;
onFileChange(event, imgType) {
this.thumbFile = <File>event.target.files[0];
}
onSubmit(){
const formData = new FormData();
formData.append('thumbimage', this.thumbFile);
this.uploadService.upload(formData).subscribe(
data => {
return data;
});
}
上传.service.ts
public upload(data) {
const headers = new HttpHeaders();
headers.append('Content-Type', 'multipart/form-data');
headers.append('Accept', 'application/json');
return this.httpClient.post<any>(`${this.SERVER_URL}/user/uploadImage`,data, {
headers: headers
})
.pipe(map((res) => {
return res;
}));
}
Laravel 项目
api.php
Route::post('user/uploadImage', "User\UserController@uploadImage");
用户控制器.php
public static function uploadImage(Request $request){
//return $request->file(); // null
//return $request; //null
}
在 laravel 请求中获取为 null。这段代码有什么问题?我想有人可以帮助我..
解决方案
推荐阅读
- excel-formula - 在 Excel 中使用 y,m,d 从年龄计算出生日期
- javascript - 向 html 添加选项
- jenkins - Jenkins“Pipeline Stage View”插件不屏蔽密码
- python - .get() 仅当 value 不是 None
- apache - TLS 1.3 和 SSLVerifyClient 不起作用,但它适用于 TLS 1.2
- c# - 如何在路径字符串中放置空格?
- recursion - 如何使用递归就地反转 Rust 字符串?
- python - 在 Windows 平台上安装 OpenEXR 时出错
- google-sheets - 谷歌表格 - 如果 A 列文本包含 B 列和 C 列中的关键字,则计数
- android - 如何使用数据库检查器获取查询执行时间?