2017-10-21 30 views
7

Đây là mã của tôi dưới đây:Làm thế nào để upload nhiều file từ laravel jqGrid

let colNames = ['ID', 'Image', 'Title', 'Description']; 
    let colModel = [     
      {name: 'id', index: 'id', width: 30, editable: true, editoptions: {size: "30", maxlength: "50"}},     
      { 
       name: 'image', 
       index: 'image', 
       align: 'left', 
       editable: true, 
       edittype: 'file', 
       editoptions: { 
        multiple: true, 
        accept: ".jpg,.png,.jpeg", 
        enctype: "multipart/form-data" 
       }, 
       width: 210, 
       align: 'center', 
       formatter: imageFormatter, 
       search: false 
      }, 
      {name: 'image_title', index: 'image_title', width: 150, editable: true, editoptions: {size: "30", maxlength: "50"}}, 
      {name: 'image_description', index: 'image_description', width: 150, editable: true, editoptions: {size: "30", maxlength: "50"}} 
     } 
    ]; 

Tôi đang sử dụng ajaxFileUpload và đây là mã của tôi để tải lên cùng:

  afterSubmit: function (response) { 
        if(response.responseJSON){ 
         $.ajaxFileUpload({ 
           url: fileuploadUrl, 
           secureuri:false, 
           fileElementId:'image', 
           dataType: 'json', 
           success: function (data, status) { 
            alert("Upload Complete."); 
           } 
          }); 
        }   
        return [true]; 
       } 
      }, 

fileElementId này được đề cập đến image. Trường hợp image chỉ được chọn một lần. Đã cố gán hình ảnh cho images[] khi chúng tôi làm hình ảnh từ HTML thuần túy. Nhưng vẫn không có may mắn như lỗi ném ajaxFileUpload.js như id không tìm thấy với images[].

Mọi trợ giúp sẽ được đánh giá cao.

+0

Có vẻ như bạn sử dụng plugin từ [phpletter] (http: //www.php letter.com/Our-Projects/AjaxFileUpload/). Trong trường hợp này tôi cần hỏi phiên bản jQuery nào được sử dụng? AjaxFileUpload là một plugin rất cũ và nó không thể làm việc với jquery mới nhất. Bạn có thể cần phải bao gồm plugin di chuyển để làm cho nó hoạt động. Tôi không chắc chắn rằng plugin này hỗ trợ nhiều tập tin tải lên. Chỉ để kiểm tra. Thử tải lên chỉ một tệp –

+0

Tải lên tệp đơn đang hoạt động tốt nhưng không nhiều. Có plugin có vẻ đã lỗi thời, bất kỳ đề xuất nào cho jQuery mới nhất được hỗ trợ để xử lý nhiều video tải lên? – Mithun

Trả lời

0
afterSubmit: function (response) { 
    if(response.responseJSON){ 
     // Simple change here 
     $('#image').attr('name', 'images[]'); 
     $.ajax({ 
      url: 'upload_files.php', // your php upload script 
      dataType: 'text', // what to expect back from the php upload script 
      cache: false, 
      contentType: false, 
      processData: false, 
      data: { 
       rowId: rowId, 
       _token: $('meta[name="csrf-token"]').attr('content')         
      }, 
      fileElementId:'image', 
      type: 'post', 
      success: function (response) { 
       alert("Upload Complete."); 
      }, 
      error: function (response) { 
       // handle any errors 
      } 
     }); 
    }   
    return [true]; 
} 

As jqgrid dynamically creates id and name, for multiple file uploads, we need array of name. Hence dynamically changed to array of name which internally handles everything in the backend.

2

Bạn có thể thử cán của riêng bạn, một cái gì đó tương tự (không kiểm tra):

afterSubmit: function (response) { 
    if(response.responseJSON){ 
     var form_data = new FormData(); 
     var count = document.getElementById('image').files.length; // assuming your file input names is image 
     for (var x = 0; x < count; x++) { 
      form_data.append("files[]", document.getElementById('image').files[x]); 
     } 
     $.ajax({ 
      url: 'upload_files.php', // your php upload script 
      dataType: 'text', // what to expect back from the php upload script 
      cache: false, 
      contentType: false, 
      processData: false, 
      data: form_data, // data created above 
      type: 'post', 
      success: function (response) { 
       alert("Upload Complete."); 
      }, 
      error: function (response) { 
       // handle any errors 
      } 
     }); 
    }   
    return [true]; 
} 
... 

Sau đó, bạn có thể truy cập các tập tin trong upload script php của bạn với $_FILES['files']

Nếu không, bạn có thể sử dụng một plugin như jQuery File Upload

+0

Điều này sẽ cần nhiều cuộc gọi http hơn, điều này không lý tưởng là – Mithun

+0

Tại sao lại có nhiều hơn thế? Chỉ cần một cuộc gọi để gửi một tập hợp các tệp bằng một cuộc gọi ajax. –

+1

@Mithun, câu trả lời của tôi chỉ sử dụng một bài đăng ajax, nó lặp qua các tệp và tạo một mảng để gửi đến tập lệnh tải lên. – CUGreen

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