file_upload.pt 2.26 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', 'mpv']
    ">
${field.start_mapping()}
  <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}">
    Your browser does not support the video tag.
  </video>


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

  <embed tal:condition="ext=='pdf'" src="${preview_url}" style="${style}">

  <a id="label-${oid}" tal:condition="preview_url" class="label label-default" href="${preview_url}"
     target="_blank"><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>