daisy
daisy

Reputation: 675

HTML5 - Drag & drop a JPEG from browser to browser

I have seen many examples how to realize drag&drop a JPEG from Desktop into my browser. Lately I heard it is also possible to drag a JPEG from another HTML-Website in to my browser and load the JPEG. How can I realize this?

Has it something to do with:

 void addElement(
   in Element element
 );

Upvotes: 2

Views: 671

Answers (1)

Xavier
Xavier

Reputation: 4017

If you are using Jquery you can find a way here : https://github.com/blueimp/jQuery-File-Upload/wiki/Drag-and-drop-uploads-from-another-web-page

here is the code to do it :

<script src="https://raw.github.com/betamax/getImageData/master/jquery.getimagedata.min.js"></script>
<script>
$(document).bind('drop dragover', function (e) {
    // Prevent the default browser drop action:
    e.preventDefault();
});
$(document).bind('drop', function (e) {
    var url = $(e.originalEvent.dataTransfer.getData('text/html')).filter('img').attr('src');
    if (url) {
        $.getImageData({
            url: url,
            success: function (img) {
                var canvas = document.createElement('canvas');
        canvas.width = img.width;
        canvas.height = img.height;
                if (canvas.getContext && canvas.toBlob) {
                    canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height);
                    canvas.toBlob(function (blob) {
                        $('#fileupload').fileupload('add', {files: [blob]});
                    }, "image/jpeg");
                }
            }
        });
    }
});
</script>

Upvotes: 2

Related Questions