Advertisement
Guest User

Untitled

a guest
Mar 30th, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.24 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Drag and Drop Documents</title>
  7.     <style>
  8.         #drop_zone {
  9.             width: 100%;
  10.             height: 200px;
  11.             border: 2px dashed #aaa;
  12.             text-align: center;
  13.             line-height: 200px;
  14.             margin-bottom: 20px;
  15.         }
  16.         #drop_zone.hover {
  17.             background-color: #f0f0f0;
  18.         }
  19.     </style>
  20. </head>
  21. <body>
  22.     <div id="drop_zone">
  23.         <p>Drag and drop documents here</p>
  24.     </div>
  25.     <input type="file" id="file_input" style="display: none;">
  26.     <button onclick="document.getElementById('file_input').click();">Browse</button>
  27.     <script>
  28.         window.onload = function() {
  29.             var dropZone = document.getElementById('drop_zone');
  30.  
  31.             dropZone.ondragover = function(e) {
  32.                 e.preventDefault();
  33.                 this.className = 'hover';
  34.             };
  35.  
  36.             dropZone.ondragleave = function() {
  37.                 this.className = '';
  38.             };
  39.  
  40.             dropZone.ondrop = function(e) {
  41.                 e.preventDefault();
  42.                 this.className = '';
  43.                
  44.                 var files = e.dataTransfer.files;
  45.                 uploadFiles(files);
  46.             };
  47.  
  48.             document.getElementById('file_input').onchange = function(e) {
  49.                 var files = e.target.files;
  50.                 uploadFiles(files);
  51.             };
  52.  
  53.             function uploadFiles(files) {
  54.                 var formData = new FormData();
  55.  
  56.                 for (var i = 0; i < files.length; i++) {
  57.                    formData.append('file', files[i]);
  58.                }
  59.  
  60.                var xhr = new XMLHttpRequest();
  61.                xhr.open('POST', '/upload');
  62.                xhr.onload = function() {
  63.                    if (xhr.status === 200) {
  64.                        console.log('Files uploaded successfully');
  65.                    } else {
  66.                        console.error('Error uploading files: ', xhr.statusText);
  67.                    }
  68.                };
  69.                xhr.send(formData);
  70.            }
  71.        };
  72.    </script>
  73. </body>
  74. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement