angular schema form image upload accepting all file types

857 views Asked by At

I have used the this plugin Angular Schema Form and all the things are working as expected but for image upload i need to have validations so as to allow the users input only images.

although the schema contains an accept attribute in the properties for image upload as follows:

{
"type": "object",
"properties": {
    "text1": {
        "type": "string",
        "title": "Untitled Text field",
        "name": "text1",
        "url": "http://localhost/image/upload/img59b7c29f1622b.jpeg"
    },
    "file2": {
        "type": "object",
        "format": "file",
        "title": "Untitled image upload field",
        "x-schema-form": {
            "type": "file",
            "accept": "png|jpg|jpeg|gif"
        },
        "name": "file2"
    }
},
"image": ["text1"]}

Still the validation does not happen and i can choose any file.

1

There are 1 answers

0
ruru On

I installed this library: https://github.com/saburab/angular-schema-form-nwp-file-upload and it works for me.

For image i used

"pattern": {"mimeType": "image/*} that accept all images.

My object is composed from:

"image": {
            "title": "Image",
            "type": "array",
            "format": "singlefile",
            "x-schema-form": {
                "type": "array"
            },
            "pattern": {
                "mimeType": "image/*,application/pdf"
            },
            "validationMessage": [
                "Only image and pdf",
                "File must be small than 2MB"
            ],
            "maxSize": {
                "maximum": "2MB"
            }
        }