vincent15000's avatar

File upload with Laravel / VueJS - JsonReponse error

Hello,

When I upload an image with a small size, it works fine.

But if the file size is too big (200 Ko doesn't work), I get this error (here is the top of the stack).

I specify that this error occurs before the validation rules are checked.

{
    "message": "Malformed UTF-8 characters, possibly incorrectly encoded",
    "exception": "InvalidArgumentException",
    "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Http/JsonResponse.php",
    "line": 90,
    "trace": [
        {
            "file": "/var/www/html/vendor/symfony/http-foundation/JsonResponse.php",
            "line": 49,
            "function": "setData",
            "class": "Illuminate\\Http\\JsonResponse",
            "type": "->"
        },
        {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Http/JsonResponse.php",
            "line": 32,
            "function": "__construct",
            "class": "Symfony\\Component\\HttpFoundation\\JsonResponse",
            "type": "->"
        },

Well ... not sure if it's really related to the file size ... I have to check ... but it's almost only for bigger images that the error occurs.

Do you have any idea ?

What do you suggest me to check ?

Thanks a lot.

V

0 likes
6 replies
experimentor's avatar

I have encountered this error many times. I check the upload with some small images. Everything works fine. But the clients click photos from their billion-pixel-camera phones and there is a 500 error!

The error description does not point to the correct problem. It is to do with the file uploads size limit in php.ini.

Please check phpinfo() to find out which ini file is being used. Edit the correct php.ini file to change these parameters :

  • upload_max_filesize = 10M
  • post_max_size = 10M

Hope this helps.

1 like
vincent15000's avatar

@vishy@serialxtech.com I have already set the upload and max file size to 100M and the error occurs for a file size of 65K.

I have also added this configuration for nginx.

client_max_body_size 100M;

And why a JsonResponse error ?

Perhaps some characters in some JPG files and not in others ? A problem when sending the datas to the backend with axios ? A serialization problem ? Something else ?

experimentor's avatar

@vincent15000 JSON response because laravel is responding to xhr request from your VUE code.

Again it is important to identify the correct php.ini file.

On my ubuntu server when I type "which php" in the terminal. It gives me the php cli's ini file. But, return phpinfo() output from a route, you will find the actual php.ini file which handles http requests. In that file, you will need to update the settings.

I ran into this specific issue about a year back. But, I'm not sure if the error message was the same. Give phpinfo() a try and see if it helps.

1 like
vincent15000's avatar
vincent15000
OP
Best Answer
Level 63

@vishy@serialxtech.com It was really not a problem with PHP or NGINX.

The problem was that Laravel was trying to read a string instead of trying to read binary data.

One solution (the one I have coded) is to send the file as base64 encoded, so it's really a string and there is no error.

1 like
experimentor's avatar

@vincent15000 Glad you found the solution. I hope there is a way to bookmark these discussions. It will come in handy when we encounter these problems.

1 like

Please or to participate in this conversation.