Drag-and-drop file upload in Google Chrome/Chromium and Safari?

后端 未结 9 1959
死守一世寂寞
死守一世寂寞 2020-11-30 17:08

Drag-and-drop file uploading can be done in Firefox 3.6.

A Google search for html5 drag-and-drop file uploading -gmail gives things like:

  • Native Drag +
相关标签:
9条回答
  • 2020-11-30 17:41

    You can use html5uploader library: http://code.google.com/p/html5uploader/

    It works with Firefox, Safari and Chrome.

    0 讨论(0)
  • 2020-11-30 17:44

    I've got something working in Chrome after much, much, much detective work. This only works on Chrome. On Safari, it freezes. On Firefox, it won't let me drop the file. IE opens the dropped file instead. Even in Chrome, the drag and drop only works once, for some reason, after which you have to refresh the page. (A possible reason for this is that something is wrong with the event handlers.)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <script type="text/javascript">
                window.onload = function () {
                    var div = document.getElementById('div');
                    div.ondragenter = div.ondragover = function (e) {
                        e.preventDefault();
                        e.dataTransfer.dropEffect = 'copy';
                        return false;
                    }
                    div.ondrop = function (e) {
                        for (var i = 0; i < e.dataTransfer.files.length; i++) { // e.dataTransfer is a DataTransfer object (https://developer.mozilla.org/En/DragDrop/DataTransfer), e.dataTransfer.files is a FileList object (https://developer.mozilla.org/en/DOM/FileList)
                            var file = e.dataTransfer.files[i]; // file is a File object (https://developer.mozilla.org/en/DOM/File)
    
                            var xhr = new XMLHttpRequest;
                            xhr.open('post', 'handler.php', true);
                            xhr.onreadystatechange = function () {
                                if (this.readyState != 4)
                                    return;
                                document.body.innerHTML += '<pre>' + this.responseText + '</pre>';
                            }
                            xhr.setRequestHeader('Content-Type', 'multipart/form-data');
                            xhr.setRequestHeader('X-File-Name', file.fileName);
                            xhr.setRequestHeader('X-File-Size', file.fileSize);
                            xhr.send(file); // For some reason sending the actual File object in Chrome works?
                        }
    
                        e.preventDefault();
                        return false;
                    }
                }
            </script>
        </head>
        <body>
            <div id="div" style="width: 100%; height: 200px; border: 1px solid blue">Drop here</div>
        </body>
    </html>
    

    handler.php:

        // This is not a true file upload. Instead, it sends the raw data directly.
        echo htmlentities(file_get_contents('php://input'));
    
    0 讨论(0)
  • 2020-11-30 17:44

    You wouldn't need to use an iframe to do pseudo ajax uploading. Chrome and Safari both support XHR2 uploads with progress events so you can do progress bars etc.

    0 讨论(0)
提交回复
热议问题