【问题标题】:Validate an Uploaded FileFormat is Excelsheet Only in Laravel 5仅在 Laravel 5 中验证上传的文件格式是否为 Excel 表格
【发布时间】:2015-12-02 16:32:45
【问题描述】:

我想验证用户只能在表单中上传 Excel 表格。为此,我在规则中尝试了 mimes:xls,xlsx 但它对我不起作用。

而且每当我编辑我的填写表格数据时,它只显示标题但不显示上传文件名。它总是告诉我没有选择文件。我已经上传了这个截图。

这方面有什么帮助吗? !!!谢谢:)

Controller.php 文件

public function store()
   {

      $uploadsheet = Request::all();

       $rules = array(
                     'title'=>'required',
                     'graph_sheet'=>'required|mimes:xls,xlsx'
                     );

       $validator = Validator::make($uploadsheet,$rules);

        if ($validator->fails())
        {
             // $messages = $validator->messages(); 
             return Redirect::to(url('/uploadsheet/create'))->withInput()->withErrors($validator);
        } else {
             $imageName='';
             if(Request::file('graph_sheet')!='') { 
               $imageName = Request::file('graph_sheet')->getClientOriginalName();
               $destinationPath = "upload/sheet/";
               Request::file('graph_sheet')->move($destinationPath, $imageName);
             }  

              Uploadsheet::create(['title' => $uploadsheet['title'],'graph_sheet' => $imageName]);
              return redirect('uploadsheet');
        }

   }

--------------- 编辑uploadsheet.blade.php 代码------

@extends('app')
@section('content')
<?php if(count($errors)!='0') { ?>
  <div class="alert alert-danger">
     <?php foreach($errors->all() as $err) { ?>  
          <?php echo $err;?><br/>        
     <?php } ?>
  </div>
<?php } ?>
    <h1>Update Content</h1>
    {!! Form::model($uploadsheet,['method' => 'PATCH','route'=>['uploadsheet.update',$uploadsheet->id],'files'=>true]) !!}
    <div class="form-group">
        {!! Form::label('Title', 'Title:') !!}
        {!! Form::text('title',null,['class'=>'form-control']) !!}
    </div>
    <div class="form-group">
        {!! Form::label('Graph Sheet', 'Graph Sheet:') !!}
        {!! Form::file('graph_sheet',null,['class'=>'form-control']) !!}
    </div>
    <div class="form-group">
        {!! Form::submit('Update', ['class' => 'btn btn-primary']) !!}
    </div>
    {!! Form::close() !!}
@stop

【问题讨论】:

  • 请与我们分享您的观点代码
  • @Saad:我已经编辑了我的代码。你可以检查一下。
  • 我看不到任何明显的问题,但仍然尝试{!! Form::file('graph_sheet') !!} 而不是{!! Form::file('graph_sheet',null,['class'=&gt;'form-control']) !!}

标签: laravel-5 laravel-validation


【解决方案1】:

正确显示您的输入字段

<div class="form-group">
{!! Form::labelControl('image','upload_Img',['class'=>'control-label col-md-3'])  !!}

<div class="col-md-4 fileinput fileinput-{{ isset($edit)  ? 'exists': 'new' }}" data-provides="fileinput">
    <div class="fileinput-new thumbnail" style="width: 200px; height: 150px;">
        {!! HTML::image(route('image','no-image.png')) !!}
    </div>
    <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px;">
        @if(isset($edit) && !empty($edit))
        {!! HTML::image($fileinput_preview) !!}
        @endif
    </div>
    <div>
        <span class="btn default btn-file">
            <span class="fileinput-new">
                {{ trans('admin/admin.select_image') }}</span>
            <span class="fileinput-exists">
                {{ trans('admin/admin.change') }}</span>
            {!! Form::file('image', null) !!}
        </span>
        <a href="javascript:;" id class="btn default fileinput-exists" data-dismiss="fileinput">
            {{ trans('admin/admin.remove') }}</a>
    </div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 2016-03-14
    • 2018-05-30
    • 1970-01-01
    • 2011-10-03
    • 2017-11-19
    • 1970-01-01
    相关资源
    最近更新 更多