Drag and drop image and store base 64 string then display



<form method="post" action="/upload">
    <input type="file"><input id="fileDragName" name="file_name"><input id="fileDragSize"><input id="fileDragType"><input id="fileDragData" name="image_base64">
    <div id="holder" style="width:200px; height:200px; border: 10px dashed #ccc" id="holder"></div>
    <input type="submit" value="Submit"></input>
  </form>
 
  <script type="text/javascript">
    function readfiles(files) {
      for (var i = 0; i < files.length; i++) {
        document.getElementById('fileDragName').value = files[i].name
        document.getElementById('fileDragSize').value = files[i].size
        document.getElementById('fileDragType').value = files[i].type
        document.getElementById('fileDragData').value = files[i].slice();
        reader = new FileReader();
        reader.onload = function(event) {
          document.getElementById('fileDragData').value = event.target.result;}
        reader.readAsDataURL(files[i]);
      }
    }
    var holder = document.getElementById('holder');
    holder.ondragover = function () { this.className = 'hover'; return false; };
    holder.ondragend = function () { this.className = ''; return false; };
    holder.ondrop = function (e) {
      this.className = '';
      e.preventDefault();
      readfiles(e.dataTransfer.files);
    }
  </script>
  <style type="text/css">
      #holder.hover { border: 10px dashed #0c0 !important; }
  </style>

  <img src="<%= @document.image_base64 %>">



Comments

Popular Posts

How to pass hash in Postman

nginx: unrecognized service

Bootstrap Select Picker append add new item if search not exist

Reading Excel Sheets using "Roo" gem in ruby on rails

Add CORS to Nginx on AWS Elastic Beanstalk

Enable gzip compression on Elastic Beanstalk with nginx

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

Get video duration by URL in Ruby on Rails

site-enables nginx setting in ruby in rails