Gabriel Gabriel - 1 month ago 6
Javascript Question

Is there a way to use default Word or PDF images/icons as the thumbnail rather than generic greyed out background?

I want to change the generic greyed out background for word/pdf files in dropzone file preview. This is the default view:

enter image description here

Which is the best way to do it?


I found a simple way to do this just now. Please note that I am using jQuery, so make sure to include that, too.

First of all, make sure your Dropzone has an id. Mine is myAwesomeDropzone:

<form id="myAwesomeDropzone" action="/upload-target" class="dropzone"></form>

Second, create image icons for each filetype you want to include. I found icons for PDF and Word and put them in a directory called img.

Then include the following JavaScript:

// Make sure to use YOUR Dropzone's ID below...
Dropzone.options.myAwesomeDropzone = {
  accept: function(file, done) {
    var thumbnail = $('.dropzone .dz-image:last');

    switch (file.type) {
      case 'application/pdf':
        thumbnail.css('background', 'url(img/pdf.png');
      case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
        thumbnail.css('background', 'url(img/doc.png');


The code above will work for PDF and Word. If you want to add more, just add more cases to the switch statement using this template:

case '[mime type]':
  thumbnail.css('background', 'url(img/[icon filename]');

Note that you can find the mime type by adding console.log(file.type); in the accept function, then drop a test file and check your browser's console.


Dropzone allows you to configure a dropzone with a configuration object in the form of Dropzone.options.[your dropzone's id]. One of the options is an accept function that is fired before a file is accepted. You can use this function's first parameter to spy on the incoming file.

That parameter has properties such as type which can tell you the mime type. Once you know the mime type, you can change the element's background image using CSS. Our element will always be $('.dropzone .dz-image:last') since we always want to target the latest dropzone file. Just change the background-image to an appropriate icon. For example, any of these will work for PDF.