【问题标题】:DropzoneJs Call to a member function getClientOriginalExtension() on nullDropzoneJs 在 null 上调用成员函数 getClientOriginalExtension()
【发布时间】:2018-02-07 09:33:31
【问题描述】:

在我开始解释我的问题之前,我必须说我阅读了很多主题 在这个网站上,他们的大部分问题是关于 enctype 但我的表格 包括在内。

问题

我可以上传我的照片at least in dropzone box view 我没有收到错误。但只要我点击保存按钮,我就会得到

"Call to a member function getClientOriginalExtension() on null"

这是我的form

{{ Form::open(array('route' => 'dropzone.store', 'method' => 'post', 'files' => true, 'class' => 'dropzone', 'id' => 'my-awesome-dropzone')) }}
             <div class="fallback">
                <input name="file" type="file" multiple />
             </div>
             <input name="imageable_id" type="hidden" value="{{$product->id}}" />

             <div class="clearfix"></div>
             {{ Form::submit('Create Product', array('class' => 'btn btn-success')) }}
            {{Form::close()}}

我的controller

public function dropzoneStore(Request $request)
  {
    $productID = $request->imageable_id;
    $product = Product::findOrFail($productID);
    // works
    $file = $request->file('file');
    $filename = 'product' . '-' . str_random(10) . '.' . $file->getClientOriginalExtension();
    $filePath = public_path('images/');
    $request->file('file')->move($filePath, $filename);


     return Image::create([
      'name' => $filename,
      'imageable_id' => $productID,
    ]);

  }

PS:错误来自$filename = 'product' . '-' . str_random(10) . '.' . $file-&gt;getClientOriginalExtension();

我在console 中的结果on success

{"name":"product-oQPyBfyZDK.jpg","imageable_id":"3","updated_at":"2018-02-07 16:30:17","created_at":"2018-02-07 16:30:17","id":13}

更新

我将 controller 代码放在 if 语句中,例如:

if ($request->hasFile('file'))
    {
        $file = $request->file('file');
        $filename = 'product' . '-' . str_random(10) . '.' . $file->getClientOriginalExtension();
        $filePath = public_path('images/');
        $request->file('file')->move($filePath, $filename);
    }

现在我得到这个错误:

未定义变量:文件名

在:

return Image::create([
      'name' => $filename,
      'imageable_id' => $productID,
    ]);

有什么想法吗?

【问题讨论】:

    标签: javascript laravel dropzone.js


    【解决方案1】:

    已解决

    我将我的回报放入 if 并且它工作正常,请查看我的更新部分以获取更多信息。

    【讨论】:

      猜你喜欢
      • 2016-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多