Skip to content
This repository has been archived by the owner on May 25, 2023. It is now read-only.

Drag and drop uploads from another web page

blueimp edited this page Apr 18, 2012 · 17 revisions

The following code snippet allows to upload images by drag&drop from another webpage:

<script src="https://raw.github.com/betamax/getImageData/master/jquery.getimagedata.min.js"></script>
<script>
$(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');
                if (canvas.getContext) {
                    canvas.getContext('2d').drawImage(img, 0, 0);
                    canvasToBlob(canvas, function (blob) {
                        $('#fileupload').fileupload('add', {files: [blob]});
                    });
                }
            }
        });
    }
});
</script>

The code snippet above makes use of the canvasToBlob JavaScript function, which is also used by the File Upload Image Processing plugin.

Due to the Same Origin policy, which also applies to the canvas element, it is not possible to load an image directly from another domain.
Therefore one of the requirements for the code snippet above is a server-side proxy to retrieve the image data: The $.getImageData library has to be included along with the jQuery File Upload libraries.

Clone this wiki locally