Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make custom preview template in dropzone js

I want to know how to make custom preview template. The documentation doesn't explain everything well, And I searched for tutorial about I didn't find anything.

Update


My html

<div id="dropzone">
  <div id="template-preview">
    <img src="assets/images/icons/plus-icon-white.png" alt="" />
    <span>Choose or drop  file from your computer</span>
    <div class="dz-preview dz-file-preview well" id="dz-preview-template">
      <div class="dz-details">
        <div class="dz-filename"><span data-dz-name></span></div>
        <div class="dz-size" data-dz-size></div>
      </div>
      <div class="dz-progress"><span class="dz-upload" data-dz-uploadprogress></span></div>
      <div class="dz-success-mark"><span></span></div>
      <div class="dz-error-mark"><span></span></div>
      <div class="dz-error-message"><span data-dz-errormessage></span></div>
    </div>
  </div>
</div>

My js

var drop = $('#dz-preview-template').html();
$('#dropzone').dropzone({
   url: "/upload",
   maxFilesize: 100,
   paramName: "uploadfile",
   maxThumbnailFilesize: 5,
   previewTemplate: drop,
   previewsContainer: "#template-preview"
});
like image 356
mohamed youssouf Avatar asked Nov 21 '16 11:11

mohamed youssouf


People also ask

How to upload files with image previews using dropzone?

Drag and drop file uploads with image previews powered by Dropzone.js. You can pass any options supported by the plugin via the data-options attribute. The value must be a valid JSON object. Copy-paste the stylesheet <link> into your <head> to load the CSS.

Why did you create dropzone?

One main goal when creating Dropzone was to have file previews that are not only practical, but also look good. That's why the default design of Dropzone looks great without you needing to do anything.

How do I react to the dropzone in a component?

Component customization props, including the strings and object literals in the custom styles props, can also be passed as functions that react to the state of the dropzone. If, for example, you pass a func instead of a node for inputContent, this function receives (files, extra), and should return the node to be rendered.


2 Answers

For me it worked with

previewTemplate: document.getElementById('preview-template').innerHTML

but I think it should be the same as using html() function in jQuery. The only thing I did differentially from your code, was to set autodiscover to false before. Maybe this helps you, too?

Dropzone.autoDiscover = false;
var uploadLogo = new Dropzone("div#uploadLogo", {
                                url: "../uploads/logo"
                                , method: "post"
                                ,...
                                ,previewTemplate: document.getElementById('preview-template').innerHTML
                                ,...
});

UPDATE

Now I think, I know what is wrong in your code. You have put the code for the template inside the dropzone div. Put it outside. Then it should work.

<div id="dropzone"></div>
<div id="template-preview">
        <div class="dz-preview dz-file-preview well" id="dz-preview-template">
                <div class="dz-details">
                        <div class="dz-filename"><span data-dz-name></span></div>
                        <div class="dz-size" data-dz-size></div>
                </div>
                <div class="dz-progress"><span class="dz-upload" data-dz-uploadprogress></span></div>
                <div class="dz-success-mark"><span></span></div>
                <div class="dz-error-mark"><span></span></div>
                <div class="dz-error-message"><span data-dz-errormessage></span></div>
        </div>
</div>

The text to display for your dropzone area, you can set during initializing the dropzone:

$('#dropzone').dropzone({
                    ...
                    , dictDefaultMessage: "Choose or drop  file from your computer"
like image 53
dns_nx Avatar answered Oct 18 '22 00:10

dns_nx


For dropzone.js version 5.5.0 you simply create a div with the id tpl then put the template inside it. When you define dropzone just set it like this:

var myDropzone = new Dropzone(
    "div#div_submit",
    {
        url: "mypage.aspx",
        previewTemplate : document.querySelector('#tpl').innerHTML
    }
);
like image 33
Izle Avatar answered Oct 17 '22 22:10

Izle