文件扩展名验证消息未显示

时间:2017-12-19 16:48:15

标签: laravel laravel-5.5

我正在尝试验证文件扩展名。 I followed this post also this one

问题

在我的测试用例中,我尝试上传了一个zip文件。错误消息是:类别图像无法上载。预期的错误消息是下面请求类中给出的消息。

我的代码如下。

申请类

class UpdateCategoryRequest extends Request
{
    public function __construct() {
    }

    public function authorize() {
        return true;
    }

    public function wantsJson() {
        return true;
    }

    public function rules() {
        return [
            'Category_Image' => "image|mimes:bmp,png,jpg,gif"
        ];
    }

    public function messages() {
        return [
            "Category_Image.mimes" =>  
                           "Only image files with extension(bmp,png,jpg,gif) are allowed."
        ];
    }
}

JQuery Ajax代码

var fileUpload = $("#Category_Image").get(0);
var files = fileUpload.files;

var fileData = new FormData();

for (var i = 0; i < files.length; i++) {
    fileData.append("Category_Image", files[i]);
}

$.ajax({
    method: "POST",
    url:    "{!! route('Update_Category') !!}",
    cache:  false,
    async:  true,
    data:   fileData,
    processData: false,
    contentType: false,
    success: function(result) {
    },
    error: function(result) {
    }
});

1 个答案:

答案 0 :(得分:0)

您的代码是正确的,您只是错过了将[]添加到Category_image名称,如下所示:

fileData.append("Category_Image[]", files[i]);