简体   繁体   English

Laravel 500错误,Base64图像,Ajax和数组

[英]Laravel 500 error, Base64 images, Ajax and array's

I am uploading, or better said, trying to upload images to my Laravel website with Ajax. 我正在上传,或者更好地说, 尝试使用Ajax将图像上传到我的Laravel网站。 I convert the images to Base64 and upload them to the server. 我将图像转换为Base64并将它们上传到服务器。 It works, sort of. 它有效。

My problem is that I get a 500 error. 我的问题是我得到500错误。 这是来自iframe服务器的响应

It's the response from the server in an Iframe 这是来自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. 唯一的问题是,一旦我这样做,我得到500错误。

I want to convert the base64 string back to an image so I want to do this: 我想将base64字符串转换回图像,所以我想这样做:

file_put_contents('foo.png', base64_decode($request->all()[0]));

But this creates the error. 但这会产生错误。 the [0] at the end of $request->all() . $request->all()结束时的[0]

I can't remove it because it needs a base64 string and not an array. 我无法删除它,因为它需要base64字符串而不是数组。 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. 我还想知道如果我在var转储中将[0]添加到$request-all()的末尾会发生什么,但我仍然得到错误。

I just don't get it. 我只是不明白。

This is my Ajax call: 这是我的Ajax调用:

    $.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. 收到的$ request值未按数字索引,因此您需要使用其名称访问它。 Instead of $request->all()[0] , you would need to use $request->theData , or whatever the value is named in the form. 而不是$request->all()[0] ,你需要使用$request->theData ,或者在表单中命名的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM