【问题标题】:Laravel Image intervention showing me Call to a member function getClientOriginalName() on a non-object LaravelLaravel 图像干预显示我调用非对象 Laravel 上的成员函数 getClientOriginalName()
【发布时间】:2015-01-30 04:50:13
【问题描述】:

我正在使用带有放置区插件的 Laravel Image Intervention 包。并且可以肯定我已经正确安装了它。当我尝试上传图片然后提交表单时,它会显示以下错误消息

“在非对象上调用成员函数 getClientOriginalName()”

如果我将此输入字段表单设为空白,即使此错误消息也会显示。在这种情况下,预计我不会向我显示任何错误消息,因为提交表单不是必填字段,但确实如此。

我有两个查询。

1) 我的代码出了什么问题

2)现在我正在尝试上传单张图片。对于多个图像,我想将文件信息存储为数组。在那种情况下,我在控制器中的代码会是什么。

这是我的实时链接,您可以从这里查看 http://thetoppinghouse.com/laravel/public/admin/index/create

http://laravel.io/bin/Jxmzo

这是我的控制器代码

public function store()
    {
        $validator = Validator::make($data = Input::all(), Index::$rules);
        if ($validator->fails())
        {
            return Redirect::back()->withErrors($validator)->withInput();
        }

        if ($validator->passes()) {
            $index = new Index;
            $index->name = Input::get('name');
            $index->category_ID = Input::get('category_ID');

            $files = Input::file('files');
            $filename = date('Y-m-d-H:i:s')."-".$files->getClientOriginalName();
            $path = public_path('img/index/' . $filename);
            Image::make($files->getRealPath())->save($path);
            $index->files = 'img/index/'.$filename;
            $index->save();

            return Redirect::route('admin.index.index')->with('message', 'Index Created');

        }

    }

//表单代码

<ul class="post-list">
     <li>
         {{ Form::label('parent_ID', 'Category') }}
         {{ Form::select('parent_ID',Category::lists('category_name','id'),Input::old('category'),array('class' => 'form-control input-sm', 'id' => 'parent_ID')) }}
      </li>

       <li>
        {{ Form::label('name', 'Index Name') }}
        {{ Form::text('name', null, array( 'class' => 'form-control input-sm', 'placeholder' => 'Name' )) }}
        {{ $errors->first('name', '<p class="error">:message</p>' ) }}
      </li>


        <li>
          {{ Form::label('image', 'Cover Image') }}
        </li>

           <div class="dropzone" id="DropzoneArea">
                <div class="fallback">
                   <input name="files" type="file" id="files" multiple>
                </div>
            </div>

          {{ Form::submit('Save') }}
        </li>
</ul

【问题讨论】:

  • Input::file('files'); 返回null ?
  • 是的......
  • 那是因为,当您尝试使用 dropzone 上传时,它会自动将图像上传到服务器,因此您的文件输入没有任何内容。你能写一些代码你如何用dropzonejs处理laravel中的图片上传吗?
  • 我只是添加了这一行。 Dropzone 完成的其余工作
    $("div#DropzoneArea").dropzone({ url: '/' });

标签: laravel image-processing dropzone.js


【解决方案1】:

使用 dropzone 处理文件上传,

 var fileDropzone = new Dropzone("div#DropzoneArea", {
    url: '/upload',          // customize the URL
    addRemoveLinks: false
 });

然后,当您上传某些内容时,上传操作将调用,您可以在该操作中处理文件上传。则可以返回上传文件的服务器文件路径。

 fileDropzone.on("success", function (file,data,e) {
    var hiddenInput = $('<input name="filePath" type="hidden value=" '+ data.path +' "">');
    // and append the hiddenInput in to the form
 });

然后上传成功后,您可以在输入hidden field 中设置上传文件的服务器路径。提交表单后,您可以通过隐藏字段值获取文件。

当您提交表单时,获取上传的文件,

$filePath = Input::input('filePath');
$file = File::get($filePath);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 2015-02-11
    • 2013-12-01
    • 2018-04-03
    • 2016-09-17
    • 2017-07-02
    相关资源
    最近更新 更多