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

标签 mysql laravel-5.5 maatwebsite-excel

用于查看和导入的 Controller

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>

这里我已经成功地在 View 页面的表格中显示了Excel文件。但是,在使用 View 页面上传文件时,我需要再次浏览该文件。现在,我需要上传文件而不再次浏览。当我使用 View 页面上的值时,它仅获取临时文件路径而不是原始文件路径。

最佳答案

所以你有

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

并获取原始文件名:

$location->getClientOriginalName();

您需要为其添加存储路径:

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

如果将其放入 session 中,您可以记住该名称一段时间:

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

关于mysql - 在 laravel 中使用 maatwebsite excel 上传之前检查导入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47129995/

相关文章:

php - Maatwerk Excel Laravel 如何设置 CSV EXPORT 分隔符?

laravel maatwebsite-excel 更改工作表方向

php - 导入 utf-8 标题 Maatwebsite Laravel

mysql - 通过 MySQL Workbench 连接到 Amazon RDS

php - "Unknown column ' tag_tag_id ' in ' 字段列表 '"多对多多态关系上的错误

mysql - 用于分组变异操作的 dbplyr 窗口函数

laravel - 如果请求输入不为空则进入数据库

php - 如何在 Guzzle http 中添加 header

mysql workbench 未选择数据库 在侧边栏的 SCHEMAS 列表中双击其名称来选择要使用的默认数据库

mysql - 红帽 6.4 上的 PHPMYAdmin