【问题标题】:Laravel Excel validationLaravel Excel 验证
【发布时间】:2016-06-11 23:37:45
【问题描述】:

我已经为 laravel 安装了 maatwebsite/excel 包。它工作正常。我的问题是我检查文件后如何处理它。

我显示到文件论坛和上传文件的上传()函数在这里:

public function upload(Request $request) {

    $orientation = $request->get('orientation');
    if($request->file('file')) {
        $status = Excel::load(Input::file('file'), function ($reader) use ($orientation) {
            return self::upload_excel_files($reader, $orientation);
        });
        if($status) {
            Session::flash('status', "Excel data was imported successfully");
            return redirect(route('admin.exchange.upload'));
        }
        else {
            return redirect(route('admin.exchange.upload'))->withErrors('Invalid format. Please check the XLS template.');
        }
    }
    return view('admin.exchange.upload');

}

public function upload_excel_files($file, $orientation) {
    if(something) return true;
    else return false;
}

upload_excel_files 函数检查文件是否具有正确的格式,如果是,它会处理它并且必须返回一个成功的注释。如果文件与要求格式不匹配,则需要给出错误消息。

不幸的是,我不确定将用户消息放在哪里。与示例中一样,它始终返回成功消息。如果由于某种原因消息在 upload_excel_files() 函数中,我没有通过第一次重定向得到消息,我在第二次重定向时得到它,这很奇怪。

你们会推荐我做什么?

【问题讨论】:

    标签: php excel laravel laravel-5.2


    【解决方案1】:

    使用 laravel 请求检查文件格式是否正确

     public function rules()
        {
            return [
                'file' => 'required|mimeTypes:'.
                    'application/vnd.ms-office,'.
                    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,'.
                    'application/vnd.ms-excel',
            ];
        }
    

    更多哑剧类型: https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types

    有关错误消息,请阅读以下内容: https://laravel.com/docs/5.4/validation#working-with-error-messages

    【讨论】:

    • 在 Laravel 8 中,验证名称现在是 mimetypes
    猜你喜欢
    • 2019-12-29
    • 2021-04-29
    • 1970-01-01
    • 2018-05-22
    • 2017-06-24
    • 2019-06-17
    • 2019-11-26
    • 2018-02-15
    • 2016-09-01
    相关资源
    最近更新 更多