Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag and drop image(s) from desktop to browser instead of searching via input

I'll be honest, way out of my depth here so any help or guidance would be great.

I have a very basic input form that allows me to choose multiple images from local folders and display them on the page as well as display info such as file type/name/size.

I'd like to be able to drag and drop images into the page, rather than manually search for the images each time. This is where I'm stuck. I have no idea what I need to google to get my desired result. I've seen a lot around the file API that I genuinely don't really understand.

Here's what I have so far: https://jsfiddle.net/umx1vpwy/

Any help would be massively appreciated!

Code:

function readImage(file) {

     var reader = new FileReader();
     var image = new Image();

     reader.readAsDataURL(file);
     reader.onload = function(_file) {
       image.src = _file.target.result; // url.createObjectURL(file);
       image.onload = function() {
         var w = this.width,
           h = this.height,
           t = file.type, // ext only: // file.type.split('/')[1],
           n = file.name,
           s = ~~(file.size / 1024) + 'KB';
         $('#uploadPreview').append('<img src="' + this.src + '"> ' + w + 'x' + h + ' ' + s + ' ' + t + ' ' + n + '<br>');
       };
       image.onerror = function() {
         alert('Invalid file type: ' + file.type);
       };
     };

   }
   $("#choose").change(function(e) {
     if (this.disabled) return alert('File upload not supported!');
     var F = this.files;
     if (F && F[0])
       for (var i = 0; i < F.length; i++) readImage(F[i]);
   });
<input type="file" id="choose" multiple="multiple" />
<div id="uploadPreview"></div>

I'd just like to say that browser compatibility shouldn't be an issue as this will just be used as a tool in Chrome or Firefox.

like image 890
Nick Avatar asked Feb 02 '16 15:02

Nick


People also ask

How do I drag and drop a picture on my desktop?

Move your cursor so it is to the top left of all the files you want to move. Left click the mouse and drag the cursor over the files until you reach the bottom right corner. Your files should be highlighted in blue to show they are selected and ready to drag.

How do you upload a picture to drag and drop?

Drag and drop file uploads happen when you drag one or more files from an underlying platform's file manager and drop them onto a web page for upload. A preview of the image indicates the upload has begun. Many JavaScript libraries create this type of drag and drop file upload feature with a few lines of code.

How do I drag and drop an image in JavaScript?

To drag an image, you simply hold the mouse button down and then move it. To drag the text, you need to highlight some text and drag it in the same way as you would drag an image.


1 Answers

This should help:

var el = document.querySelector(YOUR SELECTOR);

function onDragEnter(e) {
    e.stopPropagation();
    e.preventDefault();
}

function onDragOver(e) {
    e.stopPropagation();
    e.preventDefault();
}

function onDragLeave(e) {
    e.stopPropagation();
    e.preventDefault();
}

function onDrop(e) {
    e.stopPropagation();
    e.preventDefault();
    setFiles(e.dataTransfer.files);
    return false;
}

el.addEventListener('dragenter', onDragEnter, false);
el.addEventListener('dragover', onDragOver, false);
el.addEventListener('dragleave', onDragLeave, false);
el.addEventListener('drop', onDrop, false);

function setFiles(files){
    //Here you have your files
}
like image 152
Carsten Avatar answered Oct 22 '22 11:10

Carsten