I checked in chrome the Headers send for Form Data which looks something like this:
Now to be that looks like there is an image file named "files" in my POST request.
Now in my Controller in Lumen I do the following debug to try get the file:
return response([$request->hasFile('files'), $request->file('files'), $request->get('files')]);
However what I get is this in the response:
[true,{},null]
This is the request I make in my react app:
const formData = new FormData();
formData.append("files", this.state.productData[key][0]);
fetch(`${process.env.REACT_APP_API_URL}/products/submit`, {
method: 'POST',
body: formData,
})
Does anyone know whats wrong with what I am doing to get the image?
The $request->file()
method returns an instance of the Illuminate\Http\UploadedFile
class and appears as a {}
on your response but you're file is uploaded an is valid when $request->hasFile()
is true
so you can retrieve its properties like this:
if ($request->hasFile('files')) {
$files = $request->file('files');
return response()->json([
'path' => $files->path(),
'name' => $files->getClientOriginalName(),
'size' => $files->getSize()
]);
}
Or if you want to get response as a file:
if ($request->hasFile('files')) {
return response()->file(
$request->file('files')->path()
);
}
See Laravel docs for retrieving and storing uploaded files.
Note: I recommend to use a name other than files
because $request->files
is an instance of the Symfony\Component\HttpFoundation\FileBag
class and causes a conflict when you want to use $request->files
as a shorthand of $request->file('files')
.