if (window.FormData === undefined) { // , :-) . }
$("#drop-block").bind( // #drop-block 'dragenter', function(e) { // . }) .bind( 'dragover', function(e) { // . }).bind( 'dragleave', function(e) { // . }).bind( 'drop', function(e) { // «» . if (e.originalEvent.dataTransfer.files.length) { // - . e.preventDefault(); e.stopPropagation(); // e.originalEvent.dataTransfer.files — . // e.originalEvent.dataTransfer.files[i].size — . // e.originalEvent.dataTransfer.files[i].name — . // :-) upload(e.originalEvent.dataTransfer.files); // . } });
function upload(files) { // . // , Safari // . $.get('/blank.html'); var http = new XMLHttpRequest(); // XHR, . // if (http.upload && http.upload.addEventListener) { http.upload.addEventListener( // . 'progress', function(e) { if (e.lengthComputable) { // e.loaded — . // e.total — . // — - :-) } }, false ); http.onreadystatechange = function () { // if (this.readyState == 4) { // 4 , 4 if(this.status == 200) { // // . // , // var result = $.parseJSON(this.response); // . } else { // . } } }; http.upload.addEventListener( 'load', function(e) { // . // . // . }); http.upload.addEventListener( 'error', function(e) { // , ! }); } var form = new FormData(); // . form.append('path', '/'); // . for (var i = 0; i < files.length; i++) { form.append('file[]', files[i]); // . } http.open('POST', '/upload.php'); // . http.send(form); // , . XHR. }
if (window.FileReader === undefined) { // // — ! } // . // , . // ... var dropbox = $('#file-drag'); dropbox[0].ondragover = function() { return false; }; dropbox[0].ondragleave = function() { return false; }; dropbox[0].ondrop = function(e) { var files = e.dataTransfer.files; uploadFile(files[i]); e.preventDefault(); e.stopPropagation(); return false; }; // . // -, 0 . // -, . // function uploadFile(file) { var reader = new FileReader(); reader.onload = function() { var xhr = new XMLHttpRequest(); xhr.upload.addEventListener("progress-bar", function(e) { if (e.lengthComputable) { // -, ;-) } }, false); // load error xhr.onreadystatechange = function () { if (this.readyState == 4) { if(this.status == 200) { } else { // :-( } } }; xhr.open("POST", "upload.php"); // , . var boundary = "xxxxxxxxx"; // . xhr.setRequestHeader('Content-type', 'multipart/form-data; boundary="' + boundary + '"'); xhr.setRequestHeader('Cache-Control', 'no-cache'); // . var body = "--" + boundary + "\r\n"; body += "Content-Disposition: form-data; name='superfile'; filename='" + unescape( encodeURIComponent(file.name)) + "'\r\n"; // unescape . body += "Content-Type: application/octet-stream\r\n\r\n"; body += reader.result + "\r\n"; body += "--" + boundary + "--"; // Chrome, . if (!XMLHttpRequest.prototype.sendAsBinary) { XMLHttpRequest.prototype.sendAsBinary = function(datastr) { function byteValue(x) { return x.charCodeAt(0) & 0xff; } var ords = Array.prototype.map.call(datastr, byteValue); var ui8a = new Uint8Array(ords); this.send(ui8a.buffer); } } // . if(xhr.sendAsBinary) { // Firefox xhr.sendAsBinary(body); } else { // ( W3C) xhr.send(body); } }; // reader.readAsBinaryString(file); };
Source: https://habr.com/ru/post/154097/
All Articles