如何在laravel 5.6中固定图像输入值“ null”?

时间:2018-07-19 05:56:28

标签: php laravel dropzone.js

在我的laravel应用程序中,我正在以编程方式使用dropzone上传图像。这是我的控制器,用于将图像存储在VehicleController中

$photos = $request->file('file');
dd($photos);
        if (!is_array($photos)) {
            $photos = [$photos];
        }

        if (!is_dir($this->photos_path)) {
            mkdir($this->photos_path, 0777);
        }

        for ($i = 0; $i < count($photos); $i++) {
            $photo = $photos[$i];
            $name = sha1(date('YmdHis') . str_random(30));
            $save_name = $name . '.' . $photo->getClientOriginalExtension();//this is line 75
            $resize_name = $name . str_random(2) . '.' . $photo->getClientOriginalExtension();

            Image::make($photo)
                ->resize(250, null, function ($constraints) {
                    $constraints->aspectRatio();
                })
                ->save($this->photos_path . '/' . $resize_name);

            $photo->move($this->photos_path, $save_name);

            $upload = new Upload();
            $upload->filename = $save_name;
            $upload->resized_name = $resize_name;
            $upload->original_name = basename($photo->getClientOriginalName());
            $upload->save();
        }
        return Response::json([
            'message' => 'Image saved Successfully'
        ], 200);

而按照编程方式,jQuery是

Dropzone.autoDiscover = false;

// Dropzone class:
var myDropzone = new Dropzone("div#my-dropzone", { url: '/form'});

路线

Route::post('form','VehicleController@store');

我的刀片文件是

<div class="dropzone" id="my-dropzone"> 
    <div class="dz-message">

              <div class="col-xs-8">
                 <div class="message">
                    <p>Drop files here or Click to Upload</p>
                 </div>
              </div>
    </div>
    <div class="fallback">
        <input type="file" name="file" multiple>

    </div>
</div>

但是当我的表单提交按钮单击后,发生以下错误,

1/1) FatalErrorException

Call to a member function getClientOriginalExtension() on null
in VehicleController.php line 75

当我使用dd(photos);在

$photos = $request->file('file');

然后出现“空”按摩结果,如何解决此问题?

3 个答案:

答案 0 :(得分:4)

Please try using below AJAX Code.

<form method="post" action="{{url('form')}}" enctype="multipart/form-data">

Remove the Action(action="{{url('form')}}") from your form Tag.

var datastring = $('#fromid').serialize();  

$.ajax({  

    url: "controller url",  
                    type: 'post',  
                    contentType: "application/x-www-form-urlencoded",  
                    data: datastring,  
                    success: function(data){  
                        if(data == '100'){  
                            if($('#file').val() != ''){  
                            var form = $('#fromid')[0];   
                            var formData = new FormData(form);  
                            var imageurl='controller image upload url';

                                $.ajax({  
                                    url:imageurl,  
                                    type:"post",  
                                    data:formData,  
                                    processData:false,  
                                    contentType:false,  
                                    cache:false,  
                                    async:false,  
                                    success: function(data){  
                                        setTimeout(function(){  
                                            window.location.href="redirect url";  
                                        },4000);   
                                    }  
                                });  

                            }else{  
                                setTimeout(function(){  
                                    window.location.href="redirct url";  
                                },4000);  
                            }  

                        }else{  
                            alert(data);  
                        }   
                    }  
               });  


-------- Controller code.  
Print_r($_FILES); //You got the result.

答案 1 :(得分:0)

请在表单中添加enctype="multipart/form-data"。像这样:

<form action="" method="post" enctype="multipart/form-data">

答案 2 :(得分:0)

需要将curl属性添加为:

$ curl -sL https://jfrog.bintray.com/artifactory-debs/dists/trusty/main/binary-amd64/Packages.bz2 | bzcat | grep -A2 -F jfrog-artifactory-oss-4.3.1.deb
Filename: pool/main/j/jfrog-artifactory-oss-deb/jfrog-artifactory-oss-4.3.1.deb
SHA1: dc8b12436f309b538f6b80b4027b797e3cfce897
SHA256: 8cad6699ff17866b93a02124254ba3fb9a3ca2036048161b9bd3a49c8257831f

$ curl -sL https://jfrog.bintray.com/artifactory-debs/pool/main/j/jfrog-artifactory-oss-deb/jfrog-artifactory-oss-4.3.1.deb | sha1sum
575dc3debd016ff3a21e635625de266a6b0c2e48  -