Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.27 KB | None | 0 0
  1. <html>
  2. <head>
  3. file upload test
  4. <script
  5. src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  6.   <!-- Latest compiled and minified CSS -->
  7. </head>
  8.  
  9. <body>
  10.     <div class="container">
  11.         <div class="row files" id="files1">
  12.           <h2>Files 1</h2>
  13.           <span id="file-input-wrapper" class="btn btn-default btn-file">
  14.             Browse <input type="file" id="file-input" name="files1" multiple />
  15.           </span>
  16.           <br />
  17.           <ul class="fileList"></ul>
  18.         </div>
  19.     </div>
  20. <script type="text/javascript">
  21.  
  22. $.fn.fileUploader = function (filesToUpload) {
  23.     this.closest(".files").change(function (evt) {
  24.  
  25.         for (var i = 0; i < evt.target.files.length; i++) {
  26.            filesToUpload.push(evt.target.files[i]);
  27.        };
  28.        var output = [];
  29.  
  30.        for (var i = 0, f; f = evt.target.files[i]; i++) {
  31.            var removeLink = "<a class=\"removeFile\" href=\"#\" data-fileid=\"" + i + "\">Remove</a>";
  32.  
  33.             output.push("<li><strong>", escape(f.name), "</strong> - ",
  34.                 f.size, " bytes. &nbsp; &nbsp; ", removeLink, "</li> ");
  35.         }
  36.  
  37.         $(this).children(".fileList")
  38.             .append(output.join(""));
  39.     });
  40. };
  41.  
  42. var filesToUpload = [];
  43.  
  44. $(document).on("click",".removeFile", function(e){
  45.     e.preventDefault();
  46.     var fileName = $(this).parent().children("strong").text();
  47.      // loop through the files array and check if the name of that file matches FileName
  48.     // and get the index of the match
  49.     for(i = 0; i < filesToUpload.length; ++ i){
  50.        if(filesToUpload[i].name == fileName){
  51.            //console.log("match at: " + i);
  52.            // remove the one element at the index where we get a match
  53.            filesToUpload.splice(i, 1);
  54.         }  
  55.     }
  56.    //console.log(filesToUpload);
  57.    // remove the <li> element of the removed file from the page DOM
  58.     $(this).parent().remove();
  59.     var fileInputElement = document.getElementById('file-input');
  60.     fileInputElement.remove();
  61.     $("#file-input-wrapper").append('<input type="file" id="file-input" name="files1" multiple />');
  62. });
  63.  
  64.  
  65. $("#files1").fileUploader(filesToUpload);
  66.  
  67. $("#uploadBtn").click(function (e) {
  68.     e.preventDefault();
  69. });
  70.  
  71. </script>
  72. </body>
  73. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement