MasterRO's avatar

$request->file is empty or null with jquery ajax file uploading

Laravel 5.2.26 - $request->file is empty or null with jquery ajax file uploading

This is my JavaScript

$editorContainer.on('change', 'input[type=file]', function(){
            var $fileInput = $(this),
                file = this.files[0],
                fileType = file.type.split('/')[1];

            if($.inArray(fileType, availableFileTypes) > 0){
                var formData = new FormData;
                formData.append('image', file);
                
                $.ajax({
                    url: url,
                    type: 'POST',
                    processData: false,
                    contentType: false,
                    cache: false,
                    data: formData,
                    dataType: 'JSON',
                    beforeSend: function(){
                        $loading.removeClass('hide');
                    },
                    complete: function(){
                        $loading.addClass('hide');
                    },
                    success: function(response){
                        console.log(response);
                    },
                    error: function(e,a,b){
                        console.log(e,a,b);
                    }
                });
            }
            
            $fileInput.replaceWith($fileInput.clone(true));
        });

This is controller method that handle current ajax request

/**
     * @param Request $request
     * @return array
     */
    public function uploadImage(Request $request)
    {
        return ['all_data' => $request->all(), 'request_files' => $request->allFiles(), '$_FILES' => $_FILES, 'valid' => $request->file('image')->isValid()];
    }

And this is the result

{"all_data":{"image":{}},"request_files":{"image":{}},"$_FILES":{"image":{"name":"laravel_logo.png","type":"image\/png","tmp_name":"C:\\Windows\\Temp\\phpB2AA.tmp","error":0,"size":41650}},"valid":true}

What I had done wrong?

0 likes
9 replies
ARCANEDEV's avatar

Did you checked the $request->file('image'); ?

This is the uploaded image, as you can see the $request->file('image')->isValid() return true.

MasterRO's avatar

I tried everything. $request->file('image'); - null.

I don't understand why. I often use such file uploads, and they worked. But now I can't understand what I have done wrong or why $_FILES not empty and $request->files is empty?

jekinney's avatar

Console log your form data. Seems like your not sending any files.

MasterRO's avatar
MasterRO
OP
Best Answer
Level 8

OMG, It was my mistake, even not a mistake. I tried to return response like json with instance of UploadedFile, that has no public properties and so I could'n see it in the browser console. dd($request->file('image')); do the trick and I saw my uploaded file

1 like
heefoow's avatar

please help me automatic generate page or form from table structure of mysql and send me heefoow666@gmail. com

Please or to participate in this conversation.