首页 > 解决方案 > 将文件从 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。这段代码有什么问题?我想有人可以帮助我..

标签: angularlaravelfile-upload

解决方案


推荐阅读