문제

I'm trying to implement dropzonejs file upload on my website, but I'm getting this error

{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Call to a member function isValid() on a non-object","file":"C:\\Users\\Username\\Desktop\\localhost\\Project\\app\\controllers\\FileController.php","line":147}}

I have this inside the head tag. I check if the file is an Image and then check the dimensions before sending the file in to the controller. If the file is not an image I send it straight to the controller.

<script type="text/javascript">
    $(function() {
    Dropzone.options.createFile = {
      accept: function(file, done) {
        // checking if the filetype is an image to check the dimensions
        if ((/\.(gif|jpg|jpeg|tiff|png)$/i).test(file.name)) {              
          var pixels = 0;
          var reader = new FileReader();
          reader.onload = (function(file) {
          var image = new Image();
          image.src = file.target.result;
          image.onload = function() {
            width = this.width;
            height = this.height;
            if(width < 800 || height < 300) {
              done('image is not 800x300 pixels!'); 
            } else {
              done(); 
            } 
          }; 
          }); 
          reader.readAsDataURL(file);               
        } else {
          // if the file is not an image
          done();
        }
      },
      url: '/process/create-file',
      previewsContainer: ".dropzone-previews",
      uploadMultiple: true,
      parallelUploads: 100,
      maxFiles: 100,
    }
    });
</script>

This is the stripped controller, I removed a chunk of the code to make it shorter. Because the error is right at the top.

public function postCreateFile()
{
    if(!Input::hasFile('file'))
    return Response::json('Where is the file?', 400);

    $file = Input::file('file');
    if($file->isValid()){
        // do stuff
    }
}

if I add return var_dump($file) before the $file->isValid() line I get this response

array (size=2)  '_token' => string 'BtN7aFkEUQvXlaJDzxx28e4WMI08h5bl3VqrEaHR' (length=40)  'file' =>     array (size=1)      0 =>         object(Symfony\Component\HttpFoundation\File\UploadedFile)[9]          private 'test' => boolean false          private 'originalName' => string '65VWl.jpg' (length=9)          private 'mimeType' => string 'image/jpeg' (length=10)          private 'size' => int 260740          private 'error' => int 0

And here is the form. I have enctype and files set to true, but still I get an error with a message "non-object" as described above.

{{ Form::open(array('class' => 'dropzone', 'id' => 'create-file', 'url' => 'process/create-file', 'enctype' => 'multipart/form-data', 'files' => true)) }}
    <div class="dropzone-previews" id="giga-drop">
        <div class="fallback">
        {{ Form::file('file') }}
        </div>
    </div>
{{ Form::close() }}

Any ideas why am I getting this error?

도움이 되었습니까?

해결책

Thanks for @Maerlyn for pointing out it was an array. Also I found this link that I didn't notice when searching before Laravel and Dropzonejs, files are not uploaded correctly

I had to modify my $file variable to this:

$file = Input::file('file'); // didn't work
$file = Input::file('file')[0]; // works
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top