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
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);
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.