【问题标题】:Laravel 5.4 | How to upload image using bootstrap fileinput?Laravel 5.4 |如何使用引导文件输入上传图像?
【发布时间】:2017-04-03 15:53:01
【问题描述】:

我正在尝试使用 bootstrap-fileinput 插件在 laravel 项目中上传个人资料图片 这是风景

@section('content')

<div class="fileinput fileinput-new" data-provides="fileinput">
     <div class="fileinput-new thumbnail" style="width: 200px; height: 150px;">
          <img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&amp;text=no+image" alt="" /> </div>
               <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px;"> </div>
                    <div>
                         <span class="btn default btn-file">
                               <span class="fileinput-new"> Select image </span>
                                     <span class="fileinput-exists"> Change </span>
                                           <input type="file" name="..."> </span>
                                                   <a href="javascript:;" class="btn red fileinput-exists" data-dismiss="fileinput"> Remove </a>
                    </div>
</div>

@endsection

目标是将此图像上传到存储目录并将文件路径存储在 db 表中,但我不知道如何执行此操作,我通过 POST 将表单提交到控制器中的存储方法。 这是那个函数

public function store(Request $request)
{
    //
     $this->validate($request, [
    'first_name' => 'required',
    'dob' => 'required',
    'mobile_no' => 'unique:customers',
    'adhar_no' => 'unique:customers',

     ]);

    $customer = new customer;

    $customer->first_name = $request->first_name;
    $customer->last_name = $request->last_name;
    $customer->gender = $request->gender;
    $customer->dob = $request->dob;
    $customer->mobile_no = $request->mobile_no;
    $customer->adhar_no = $request->adhar_no;
    $customer->street = $request->street;


    $customer->save();
    return redirect('home/customer');

}

请帮忙

【问题讨论】:

    标签: twitter-bootstrap file-io laravel-5 laravel-5.4


    【解决方案1】:

    使用 laravel 5.4 从请求中获取文件真的很容易。在您的情况下,您可以使用:

    $file = $request->file('file');
    

    或者:

    $file = $request->file;
    

    要存储文件,您可以使用store 方法,如下所示:

    $path = $request->file->store('the/path');
    

    之后,您可以将返回的路径 ($path) 与其他客户详细信息一起存储在您的数据库中。

    有关更多详细信息,请访问此页面:Retrieving Uploaded Files,如果您想在处理之前验证文件,请参阅:Validation Image Rule

    我希望这会对你有所帮助。祝你好运。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-19
      • 1970-01-01
      • 2016-06-17
      • 2017-07-06
      • 2020-04-22
      • 2018-02-04
      • 1970-01-01
      • 2017-08-02
      相关资源
      最近更新 更多