Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag and drop input file [duplicate]

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?

like image 380
prgrm Avatar asked Feb 05 '23 01:02

prgrm


1 Answers

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>
like image 156
MKAD Avatar answered Feb 07 '23 14:02

MKAD