2014-04-03 28 views
5

Tôi muốn hỏi làm thế nào để thêm các tập tin trong loại đầu vào tập tin nhiều khi người dùng chọn hình ảnh nhiều hơn một thời gian trước khi tải lên ví dụ tôi chọn 2 hình ảnh và một lần nữa tôi chọn 3 hình ảnh lần thứ hai trước khi tải lên, hai tệp đầu tiên bị xóa khỏi tệp loại đầu vào và 3 hình ảnh cuối cùng vẫn ở đó tôi biết hành vi mặc định của tệp đầu vào là nó thay thế tệp mới bằng những tệp mới được chọn nhưng tôi muốn giữ tất cả các tệp người dùng chọn nhiều lần trước khi tải lên như thế nào là nó có thể tôi đang sử dụng ajax và formdata nhưng nó không hoạt động xin vui lòng giúp tôi. Cảm ơnlàm thế nào để thêm các tập tin trong loại đầu vào tập tin nhiều trước khi tải lên

Trả lời

2

tôi có mặt cùng một vấn đề như bạn

bây giờ tôi đã tìm thấy giải pháp cho rằng

<input type="file" id="attachfile" name="replyFiles" multiple> <!--File Element for multiple intput--> 
<div id="#filelist"></div> 
<script> 
    var selDiv = ""; 
    var storedFiles = []; //store the object of the all files 

    document.addEventListener("DOMContentLoaded", init, false); 

    function init() { 
     //To add the change listener on over file element 
     document.querySelector('#attachfile').addEventListener('change', handleFileSelect, false); 
     //allocate division where you want to print file name 
     selDiv = document.querySelector("#filelist"); 
    } 

    //function to handle the file select listenere 
    function handleFileSelect(e) { 
     //to check that even single file is selected or not 
     if(!e.target.files) return;  

     //for clear the value of the selDiv 
     selDiv.innerHTML = ""; 

     //get the array of file object in files variable 
     var files = e.target.files; 
     var filesArr = Array.prototype.slice.call(files); 

     //print if any file is selected previosly 
     for(var i=0;i<storedFiles.length;i++) 
     { 
      selDiv.innerHTML += "<div class='filename'> <span> " + storedFiles[i].name + "</span></div>"; 
     } 
     filesArr.forEach(function(f) { 
      //add new selected files into the array list 
      storedFiles.push(f); 
      //print new selected files into the given division 
      selDiv.innerHTML += "<div class='filename'> <span> " + f.name + "</span></div>"; 
     }); 

     //store the array of file in our element this is send to other page by form submit 
     $("input[name=replyfiles]").val(storedFiles); 
} 
</script> 

này đang làm việc trong trang của tôi đúng tôi muốn điều này cũng được thể hữu ích cho bạn cũng

+0

Cảm ơn rất nhiều cho câu trả lời của bạn tôi sẽ cố gắng giải pháp này. Cảm ơn – Aisha

0

bạn có thể cài đặt bootstrap-fileinput, nó có tất cả các chức năng mà bạn muốn, với thiết kế tuyệt vời.

4

Tôi biết rằng tôi đang vi phạm các quy tắc nhưng answer được top bình chọn cho câu hỏi này là sai. Tôi không có đủ danh tiếng để bình luận, đó là lý do tại sao tôi viết nó như một câu trả lời khác. Dòng này $("input[name=replyfiles]").val(storedFiles); ném một lỗi as type đầu vào tập tin không thể được sửa đổi thông qua javascript (trừ thiết lập lại nó)

0

Cách đúng là sử dụng $("input[name=replyfiles]").clone().appendTo($("input[name=replyfiles]").parent()).val(''); $("input[name=replyfiles]").hide(); trừ $("input[name=replyfiles]").val(storedFiles);.

Các vấn đề liên quan