Dropzone和非对象错误

时间:2014-02-09 12:33:40

标签: php jquery laravel laravel-4 dropzone.js

我正在尝试在我的网站上实现dropzonejs文件上传,但我收到此错误

{"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}}

我在head标签里面有这个。我检查文件是否是图像,然后在将文件发送到控制器之前检查尺寸。如果文件不是图像,我将其直接发送给控制器。

<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>

这是剥离的控制器,我删除了一大块代码以缩短它。因为错误位于顶部。

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

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

如果我在return var_dump($file)行之前添加$file->isValid(),我会收到此回复

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

这是表格。我有enctype和文件设置为true,但我仍然收到错误消息“非对象”如上所述。

{{ 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() }}

任何想法我为什么会收到此错误?

1 个答案:

答案 0 :(得分:2)

感谢@Maerlyn指出它是一个数组。 此外,我发现在Laravel and Dropzonejs, files are not uploaded correctly

之前搜索时我没有注意到这个链接

我必须将我的$ file变量修改为:

$file = Input::file('file'); // didn't work
$file = Input::file('file')[0]; // works