Editor -File Upload validation with MIME type

Editor -File Upload validation with MIME type

solucionessoluciones Posts: 12Questions: 5Answers: 0

Hi,

I need to use MIME type to check file content validation in file upload.

How can I implement it?

Replies

  • allanallan Posts: 63,680Questions: 1Answers: 10,498 Site admin

    A custom validator would be the way to do this. $file['type'] will give you the MIME type.

    Allan

  • solucionessoluciones Posts: 12Questions: 5Answers: 0

    But how to validation MIME type before upload the file?

    For example, in preSubmit event? How to access image properties?

    editor.on( 'preSubmit', function ( e, o, a ) {

    var files =this.val( 'trabajos.file_id' );

  • allanallan Posts: 63,680Questions: 1Answers: 10,498 Site admin

    The preUpload event is how to do this on the client-side. It will give the mime type as part of the third parameter.

    Remember to check it on the server-side as well though, since client-side validation can always be bypassed.

    Allan

  • solucionessoluciones Posts: 12Questions: 5Answers: 0

    And how can I check it on the server-side?

    Can you give me an example for MIME TYPE? I have validations for size and extensions

    ->validator( function ( $file ) {

                    return $file['size'] >= 500000 ?
                        "Files must be smaller than 500K" :
                        null;
                } )
                ->allowedExtensions( array( 'png', 'jpg', 'gif','jpeg' ), "Please upload an image" )
    

    Thanks,

  • allanallan Posts: 63,680Questions: 1Answers: 10,498 Site admin

    Almost exactly the same as your file size validator:

    ->validator( function ( $file ) {
        return $file['type'] !== ... ?
            "Wrong file type" :
            null;
    } )
    

    Allan

  • solucionessoluciones Posts: 12Questions: 5Answers: 0

    I have this code

    return $file['type'] !== 'image/jpeg' or $file['type'] !== 'image/jpg' ?
    "Wrong file type " . $file['type'] :
    null;

    But I have uploaded a .svg image renamed as .jpg and allows it

  • allanallan Posts: 63,680Questions: 1Answers: 10,498 Site admin

    If you debug the code, what is it giving $file['type'] as?

    Allan

This discussion has been closed.