file_upload.pt 2.29 KB
<tal:block tal:define="oid oid|field.oid;
    css_class css_class|field.widget.css_class;
    style style|field.widget.style;
    preview_url cstruct.get('preview_url')|'';
    ext str(cstruct.get('filename').split('.')[-1:][0]).lower()|[];
    fname str(cstruct.get('filename'))|'';
    delete cstruct.get('delete')|'';
    maxsize field.widget.size|5242880;
    img ['jpg', 'jpeg', 'gif', 'png', 'svg', 'eps', 'psd'];
    video ['avi', 'mov', 'mp4']
    ">
  ${field.start_mapping()}
  <div class="thumbnail" tal:condition="ext in img or ext in video or ext=='pdf'">
    <img tal:condition="ext in img" id="preview-${oid}" alt="" src="${preview_url}" style="${style}"
      onload="window.URL.revokeObjectURL(this.src);"></img>
    <video tal:condition="ext in video" style="${style}" controls src="${preview_url}" type="video/${ext}"
      id="preview-${oid}">
      Your browser does not support the video tag.
    </video>

    <video tal:condition="ext=='mp4'" controls style="${style}" src="${preview_url}" type="video/mp4"
      id="preview-${oid}">
      Your browser does not support the video tag.
    </video>
    <embed tal:condition="ext=='pdf'" src="${preview_url}" style="${style}" id="preview-${oid}">
  </div>

  <a tal:condition="ext" class="label label-default" href="${preview_url}" target="_blank" id="a-preview-${oid}"><i
      class="fa fa-search"></i> View</a>

  <input type="file" name="upload" id="${oid}" tal:attributes="style style;
          accept accept|field.widget.accept;
          data-filename fname;
          attributes|field.widget.attributes|{};" />
  <input tal:define="uid cstruct.get('uid')" tal:condition="uid" type="hidden" name="uid" value="${uid}" />
  ${field.end_mapping()}
  <script type="text/javascript">
    deform.addCallback('${oid}', function (oid) {
      $('#' + oid).upload();
    });
    document.getElementById("${oid}").onchange = function () {
      if (this.files[0].size > ${ maxsize }) {
        alert("File is too big!");
        this.value = "";
        document.getElementById('preview-' + this.id).src = '';
      }
      document.getElementById('preview-' + this.id).src = window.URL.createObjectURL(this.files[0]);
      document.getElementById('labeldelete-' + this.id).remove();
      document.getElementById('label-' + this.id).remove();
    };
  </script>
</tal:block>