I am uploading, or better said, trying to upload images to my Laravel website with Ajax. I convert the images to Base64 and upload them to the server. It works, sort of.
My problem is that I get a 500 error.
It's the response from the server in an Iframe
The thing is, I do not get this error all the times. I only get it when I try to do something with the response.
This is my method where I handle the upload. When I run it like this everything works and I do not get the error.
public function upload(Request $request)
{
return var_dump($request->all());
}
But it is an array. So to do something with it I have to specify the key. Only problem is that as soon I do that I get the 500 error.
I want to convert the base64 string back to an image so I want to do this:
file_put_contents('foo.png', base64_decode($request->all()[0]));
But this creates the error. the [0]
at the end of $request->all()
.
I can't remove it because it needs a base64 string and not an array. I also wanted to know what would happen if I just add [0]
to the end of $request-all()
in the var dump but I still got the errror.
I just don't get it.
This is my Ajax call:
$.ajax({
url: "/admin/upload",
type: "POST",
data: queue,
processData: false,
error: function(xhr, status, error) {
let err = xhr.responseText;
//console.log(err);
$('#upload-InnerPanel').append("<iframe width='600' height='500' src='" + err +"'> </iframe>")
},
success: function (xhr) {
console.log(xhr);
},
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
Answered in the comments, but posting this as an answer anyway:
The $request value that is received isn't numerically indexed, so you need to access it using its name. Instead of $request->all()[0]
, you would need to use $request->theData
, or whatever the value is named in the form.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.