Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict Dropzone to upload only specific type of files

I am uploading files with Dropzone here is my code

<div>
    <form id="mainDiv" class="dropzone needsclick" enctype="multipart/form-data" method="post" action="uploadFiles?type=5" role="form">

        <div class="dz-message needsclick">
            Drop files here or click to upload.<br />
            <span class="note needsclick">(Please upload <strong>PDF, JPG, GIF, PNG, PDF</strong> files only.)</span>
        </div>

    </form>
</div>


<div>
    <form id="recommendationDiv" class="dropzone needsclick" enctype="multipart/form-data" method="post" action="uploadFiles?type=5" role="form">

        <div class="dz-message needsclick">
            Drop files here or click to upload.<br />
            <span class="note needsclick">(Please upload <strong>PDF, JPG, GIF, PNG, PDF</strong> files only.)</span>
        </div>

    </form>
</div>

Uploading works just fine however i want to restrict the type of upload documents

<script>

  Dropzone.options.dropzone = {
        acceptedFiles:'image/*'       
    };


</script>  

Accepted files doesnt seem to be working , it just uploades everything.

like image 282
Muhammad Umar Avatar asked Jan 05 '18 15:01

Muhammad Umar


2 Answers

You need to include the camelized ID of the dropzone element. For your example, you have the <form> with id="recommendationDiv" so you would set the options with:

Dropzone.options.recommendationDiv = {
    acceptedFiles: 'image/*'
};

You are setting the options for a form with id "dropzone" that doesn't exist. After setting with the correct id, you should see the correct behavior:

Dropzone refusing file

See Dropzone's configuration documentation.

like image 79
zero298 Avatar answered Nov 09 '22 23:11

zero298


if you want to check any other file format not only image like csv ,pdf

accept: function(file, done) {

        if (file.type !== "text/csv") {
            done("File Type is not csv !");
        } else {
            done();
        }
    }
like image 26
Muhammad Amir Avatar answered Nov 09 '22 23:11

Muhammad Amir