在 laravel 中使用 maatwebsite excel 上传之前检查导入文件

Review import file before uploading using maatwebsite excel in laravel

用于查看和导入的控制器

public function show(Request $request){
    $location=$request->file('import_file');
    if($request->hasFile('import_file')){
        $data =  Excel::selectSheets('Sheet1')->load($request->file('import_file'))->noHeading()->toArray();
        $result = array(
            'data'           =>$data,
            'page_header'    =>'Review Uploaded file',
            'location'       =>$location,  
      );
    }
            return view('admin.floorsheet.list', compact('result'));   

}

这里我得到了文件的位置,但它只获取临时位置而不是原始文件路径。

public function store(Request $request)
{
    if($request->hasFile('import_file')){
        Excel::load($request->file('import_file')->getRealPath(), function ($reader) {
            foreach ($reader->noHeading()->first()->toArray() as $key => $row) {
                $data['transaction'] = $row[1];
                $data['symbol'] = $row[2];
                $data['buyer'] = $row[3];
                $data['seller'] = $row[4];
                $data['quantity'] = $row[5];
                $data['rate'] = $row[6];
                $data['amount'] = $row[7];


                if(!empty($data)) {
                    DB::table('tbl_floorsheet')->insert($data);


                }
            }
        });
    }

    $result = array(

            'page_header'    =>'File Uploaded',
            'date'          =>date('Y-m-d'),
      );
            return view('admin.floorsheet.import',compact('result'));   
}

我的索引页

<form class="form-horizontal" method="POST" action="{{ route('/my-admin/floorsheet.show') }}" enctype="multipart/form-data">
            {{ csrf_field() }}
            <div class="col-lg-9 col-md-9 col-sm-12 col-xs-12">
                <div class="form-group">
                    <label for="file">Upload Filer</label>
                    <input type="file" class="form-control" name="import_file" >

                    <label for="acceptdate">Date</label>
                        <input type="text" class="form-control" id="operationdate" name="operationdate" value="{{ $result['date'] }}">

                                       </div>
            </div>
            <div class="clearfix"></div>

            <div class="clearfix"></div>
            <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
                <div class="form-group">
                    <button type="submit" class="btn btn-success">Upload File</button>
                    <button type="reset" class="btn btn-danger">Reset</button>
                </div>
            </div>
        </form>

我的列表页

<form class="form-horizontal" method="POST" action="{{ route('floorsheet.store') }}" enctype="multipart/form-data">
            {{ csrf_field() }}
            <div class="col-lg-9 col-md-9 col-sm-12 col-xs-12">
                <div class="form-group">

                    <label for="file">Upload Filer</label>
                    <input type="file" class="form-control" name="import_file" value="{{$result['location']}}">


                   </div>

                   <table class="table table-hover table-responsive table-condensed" >
                            <tr>
                                <th>S.No</th>
                                <th>Transcation No.</th>
                                <th>Symbol</th>
                                <th>Buyer</th>
                                <th>Seller</th>
                                <th>Quantity</th>
                                <th>Rate</th>
                                <th>Amount</th>

                            </tr>

                             @foreach($result['data'] as $d)
                               <tr>
                             @foreach($d as $v)
                            <td>{{$v}}</td>
                              @endforeach
                              </tr>
                              @endforeach
                    </table>
            </div>
            <div class="clearfix"></div>

            <div class="clearfix"></div>
            <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
                <div class="form-group">
                    <button type="submit" class="btn btn-success">Upload File</button>
                    <button type="reset" class="btn btn-danger">Reset</button>
                </div>
            </div>
        </form>

这里我已经在查看页面中成功显示了 table 中的 excel 文件。但是在使用视图页面上传文件时,我需要再次浏览文件。现在,我需要上传文件而无需再次浏览。当我在视图页面上使用该值时,它只获取临时文件路径而不是原始文件路径。

所以你有

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

并获取原始文件名:

$location->getClientOriginalName();

需要添加路径存放:

$destinationPath = storage_path() .'/uploads'; $destinationName = date("YmdHis") .'_'. $location->getClientOriginalName(); $destinationFQN = "$destinationPath/$destinationName";

这个名字放在session里可以记住一会:

session()->put('filename', $location->getClientOriginalName()); $value = session()->get('filename'); session()->forget('filename');