DimZ's avatar
Level 4

Receive file upload through API route

Hello,

I'm trying to send a file from the Angular 4 frontend to Laravel backend, but I have nothing. I've tried with postman too.

Here is my code :

routes/api.php :

Route::put('user/{id}/profilePicture', 'UserController@profilePicture')->name('user.profilePicture');

UserController :

    public function profilePicture($id, Request $request)
    {
        // $validation = $this->validate(request(), [
        //     'test' => 'required',
        //     'profile_picture' => 'image',
        // ]);

        \Log::debug($request->all());
        \Log::debug($request->file('profile_picture'));
    }

Postman :

Thanks for your help

0 likes
3 replies
DimZ's avatar
Level 4

On Postman I simply send a form-data with 2 values :

  • test : "okay"
  • profile_picture (file type) : myFileChoosen.jpg

With Angular, I did that :

var formData: FormData = new FormData();
formData.append("test", "okay");
formData.append("profile_picture", file, file.name);
this._userService.uploadFile(this.user.id, formData);
DimZ's avatar
Level 4

Hmm sorry, That was an Angular4 error, from their HttpClientModule. The Content-Type was not correctly set (see this issue).

Thanks!

Please or to participate in this conversation.