4

I have a pretty standard form to submit an image:

<form enctype="multipart/form-data" class="form-horizontal" role="form" method="POST">
<input id="image" name="image" type="file"/>
</form>

I want to be able to drag an image to an area so it will selected as the input.

I have researched on the internet how to do such simple task but I only get overdone plug-ins that use AJAX which sadly is not an option for this form. Anyone knows how to do this?

prgrm
  • 3,734
  • 14
  • 40
  • 80
  • This has been answered pretty thoroughly here. The only thing you'll need, it looks like is jquery, no ajax: http://stackoverflow.com/a/12713396/1836515 – Triclops200 Feb 20 '17 at 15:57
  • 1
    There is a lot of good librairies to do so, i use [dropzoneJS](http://www.dropzonejs.com/) which is pretty cool, you should check it out. – Nicolas Feb 20 '17 at 16:04
  • Refer to this answer, tested and working: http://stackoverflow.com/a/24470646/6826238 – wpcoder Feb 20 '17 at 16:22

1 Answers1

9

just drag your image into a input. if you need some information how to work with the result (link / title / src or something like that) of your droped image just visit this site.

function handleFileSelect(evt) {
    evt.stopPropagation();
    evt.preventDefault();

    var files = evt.dataTransfer.files; // FileList object.

    // files is a FileList of File objects. List some properties.
    var output = [];
    for (var i = 0, f; f = files[i]; i++) {
      output.push('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ',
                  f.size, ' bytes, last modified: ',
                  f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a',
                  '</li>');
    }
    document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';
  }

  function handleDragOver(evt) {
    evt.stopPropagation();
    evt.preventDefault();
    evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
  }

  // Setup the dnd listeners.
  var dropZone = document.getElementById('drop_zone');
  dropZone.addEventListener('dragover', handleDragOver, false);
  dropZone.addEventListener('drop', handleFileSelect, false);
.example {
    padding: 10px;
    border: 1px solid #ccc;
}

#drop_zone {
    border: 2px dashed #bbb;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    border-radius: 5px;
    padding: 25px;
    text-align: center;
    font: 20pt bold 'Vollkorn';
    color: #bbb;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="example">
    <div id="drop_zone">Drop files here</div>
    <output id="file_list2"></output>
  </div>
  
  
  <output id="list"></output>
  <br>
  <br>
  <br>

<p> Easy solution </p>
  <div class="intro-text">
    <input class="" type="file" id="file-input" accept="image/*" capture="" name="files[]" multiple="">
  </div>
MKAD
  • 447
  • 2
  • 10