1

I'm using Drag and Drop to upload images, but how can I preview my image before saving?

var obj = $('.drop');
obj.on('drop', function(e){
    e.stopPropagation();
    e.preventDefault();
    $(this).css('border',"2px dotted #bdc3c7");
    var files = e.originalEvent.dataTransfer.files;
    var file = files[0];
});

I've been looking around and found about FileReader but I have no idea how to implement

Rory McCrossan
  • 306,214
  • 37
  • 269
  • 303
BrianCas
  • 529
  • 2
  • 9
  • 20

1 Answers1

0

Please try

<form id="form1" runat="server">
    <input type='file' id="imgInp" />
    <br>
    <img id="blah" src="http://i.imgur.com/zAyt4lX.jpg" alt="your image" height="100" />
</form>

And your JS part :

function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#blah').attr('src', e.target.result);
            }

            reader.readAsDataURL(input.files[0]);
        }
    }

    $("#imgInp").change(function(){
        readURL(this);
    });

I hope it helps. Please take a look on this fiddle

Vineet
  • 4,279
  • 3
  • 19
  • 42