【问题标题】:Dropzone and non-object errorDropzone和非对象错误
【发布时间】:2014-03-06 17:08:00
【问题描述】:

我正在尝试在我的网站上实现 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
    }
}

如果我在 $file-&gt;isValid() 行之前添加 return var_dump($file) 我会收到此响应

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 和 files 设置为 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() }}

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

【问题讨论】:

  • $file 是一个数组(如var_dump 输出所示),您正试图在其上调用一个方法,就好像它是一个对象一样 - 这是您的错误。
  • 关于如何解决此问题的任何想法?
  • 没用过 laravel,我猜文档里有?

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


【解决方案1】:

感谢@Maerlyn 指出它是一个数组。 我还发现了在Laravel and Dropzonejs, files are not uploaded correctly之前搜索时没有注意到的这个链接

我不得不将我的 $file 变量修改为:

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

【讨论】:

    猜你喜欢
    • 2013-12-22
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 2011-02-07
    • 1970-01-01
    相关资源
    最近更新 更多