【问题标题】:Laravel Mimetypes Validation Always FalseLaravel Mimetypes 验证总是错误的
【发布时间】:2017-08-08 13:52:52
【问题描述】:

我正在上传一个文件并想检查它是否与以下内容匹配:

public function rules()
    {
        return ['file' => 'required|mimetypes:
                                    application/vnd.ms-excel,
                                    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,
                                    application/pdf,
                                    text/plain,
                                    text/csv,
                                    csv,
                                    txt,
                                    text/tsv,
                                    image/jpeg,
                                    image/png,
                                    image/svg+xml,
                                    image/tiff,
                                    video/x-msvideo,
                                    video/mpeg
                                    |max:12345678'];
}

但是,它总是返回“The file must be a file of type: application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/pdf, text/plain, text/csv, csv, txt, text/tsv, image/jpeg, image/png, image/svg+xml, image/tiff, video/x-msvideo, video/mpeg.

mimetype 的猜测似乎是正确的。比如我尝试上传foobar.csv

$request->file->getMimeType() // text/plain

但是,它仍然返回The file must be a file of type [...]

我在这里错过了什么?

【问题讨论】:

    标签: laravel validation mime-types


    【解决方案1】:

    试试下面的代码,根据RFC 2616 7.2.1添加了'application/octet-stream'。

    public function rules()
        {
            return ['file' => 'required|mimetypes:
                                        application/vnd.ms-excel,
                                        application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,
                                        application/pdf,
                                        text/plain,
                                        text/csv,
                                        application/octet-stream,
                                        csv,
                                        txt,
                                        text/tsv,
                                        image/jpeg,
                                        image/png,
                                        image/svg+xml,
                                        image/tiff,
                                        video/x-msvideo,
                                        video/mpeg
                                        |max:12345678'];
    }
    

    【讨论】:

      【解决方案2】:

      问题在于mimetypes 被指定为字符串。在字符串中,为了代码清晰,我添加了新行,这些行被解释为这样。当我删除它们以将所有内容放在一行中时,它起作用了:

      return ['file' => 'required|mimetypes:application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/pdf,application/octet-stream,text/plain,text/csv,csv,txt,text/tsv,image/jpeg,image/png,image/svg+xml,image/tiff,video/x-msvideo,video/mpeg|max:12345678'];

      【讨论】:

        猜你喜欢
        • 2017-09-01
        • 1970-01-01
        • 2014-01-21
        • 1970-01-01
        • 2019-06-06
        • 2020-11-03
        • 1970-01-01
        • 2018-09-05
        相关资源
        最近更新 更多