I have a problem with an image input. it shows nice, with their styles, and i checked by JS that on change the image the input fills himselves with the image. but when i submit the form, the avatar input doesn't appear. i have the form with enctype="multipart/form-data", and i tried with a normal input image and it works fine.
Hi Jordi,
After checking, for some reason, the image data has been removed from the input form. Could you please try with the changes as a workaround?
In the /metronic/core/components/image-input/image-input.ts
Inside _change()
function:
Please try to remove
this._inputElement.value = "";
Hi Jordi,
Add logging to your Laravel controller to confirm whether the file is being received properly. Here's an example:
if ($request->hasFile("avatar")) {
Log::info("Avatar received:", ["avatar" => $request->file("avatar")->getClientOriginalName()]);
} else {
Log::warning("Avatar not received.");
}
document.getElementById("avatar").addEventListener("change", function(event) {
const file = event.target.files[0];
if (file) {
console.log("File selected:", file.name);
}
});
Hi!
About the Controller. it gets that: [2024-08-08 15:50:30] local.WARNING: Avatar not received.
On javascript, i did what you sent and i got this:
File selected: 300-6.png
So as you can see, the input works fine but it is not going to the controller. there is some way. The form is this:
<pre lang="html>
<form method="POST" action="{{ route('profile.update') }}" class="mt-6 space-y-6" enctype="multipart/form-data" >
@csrf
@method('PUT')
</pre>
And this is the input complete:
<div class="image-input size-16" data-image-input="true" >
<input name="avatar_remove" type="hidden" />
<input accept=".png, .jpg, .jpeg" name="avatar" type="file" />
...
Hi Jordi,
Check the browser console for any JavaScript errors or warnings.
Inspect the form data being sent to the server. You can do this in the browser's network tab to ensure the file input is included in the form data.
Log the received data on the server side to verify that the file is being received.
Verify that your JavaScript code correctly updates the input value and that no JavaScript errors are occurring during form submission.
Sample code:
document.getElementById("avatar").addEventListener("change", function(event) {
const file = event.target.files[0];
if (file) {
// Ensure file is handled correctly
console.log("File selected:", file.name);
}
});
It shows like it is sent:
_token: Vt5xOByhvgciF41Ox3VcXOmqelDbPFuVt2BrNKmx
_method: PUT
avatar_remove:
avatar: (binario)
name: Jordi
email: jordi_lario@hotmail.com
But at DD on laravel avatar disappears. If i put a normal input file it works fine.