2016-01-24 118 views
0

我想在Dropzone.js插件的幫助下使用AJAX上傳多個文件。Dropzone.js + Laravel - 多文件上傳不起作用(僅適用於一個文件)

我所做的是 -

HTML(視圖) -

<div class="dropzone" id="add-slide-image"> 
</div> 

JS-

Dropzone.autoDiscover = false; 
var myDropzone = new Dropzone("div#add-slide-image", 
        { 
         url: baseUrl+"/slide/uploadimage", 
         params: { 
          _token: csrfToken 
         }, 
         maxFilesize: 3, // MB 
         maxFiles:40,     //Trigers "maxfilesexceeded" event 
         acceptedFiles:'image/*', 
         method: "post", 
         addRemoveLinks: true, 
         paramName: "file", 
         uploadMultiple: true 
        }); 

控制器 -

public function storeImageFromClientComputer(Request $request) 
{ 
    //return $request->file('file');//Input::file('droped_images'); 
     $files = $request->file('file'); 
     $names = []; 

     foreach ($files as $file) 
     { 
     $names = $file->getClientOriginalName(); 
     } 
     return $names; 
    /*//$files=$request->file('images'); 
    $file=$request->file('file'); 

    if($file) 
    { 
     $random_original_image_name=str_random(30).".".$file->getClientOriginalExtension(); 
     $file->move(public_path().'/uploads/userfiles/images/',Auth::id()."-".$random_original_image_name); 
     $original_image_name=base_path()."/public/uploads/userfiles/images/".Auth::id()."-".$random_original_image_name; 
     $thumb_image_name=base_path()."/public/uploads/userfiles/thumbs/".Auth::id()."-".$random_original_image_name; 
     if(file_exists($original_image_name)){ 
      $thumb=Image::make($original_image_name)->resize(200,null,function($constraint){ 
       $constraint->aspectRatio(); 
      }); 
      $thumb->save($thumb_image_name); 

      $id=UserSlidePhoto::insertGetId([ 
       'user_id'=>Auth::id(), 
       'social_image_id'=>0, 
       'original_image_name'=>Auth::id()."-".$random_original_image_name, 
      'thumbnail_name'=>Auth::id()."-".$random_original_image_name 
      ]); 
      $image_info=UserSlidePhoto::find($id); 
      return response()->json($image_info,200); 
     } 
    } 
    return response()->json("ERROR",400);*/ 
} 

該控制器僅用於測試如果所有文件都被接收VED。 我只從這裏獲得一個文件。 評論代碼是用於真實文件上傳。

我總是隻有一個文件在循環中。

Dropzone的CDN是從here(CSS + JS)添加的。

任何人都可以請幫忙嗎?

預先感謝幫助:)

回答

0

我已經解決了它像這個 -

JS-

Dropzone.autoDiscover = false; 
var myDropzone = new Dropzone("div#add-slide-image", 
        { 
         url: baseUrl+"/slide/uploadimage", 
         params: { 
          _token: csrfToken 
         }, 
         maxFilesize: 3, // MB 
         maxFiles:40, 
         parallelUploads: 10000, 
         acceptedFiles:'image/*', 
         method: "post", 
         addRemoveLinks: true, 
         paramName: "file", 
         uploadMultiple: true 
        }); 

控制器(PHP) -

public function storeImageFromClientComputer(Request $request) 
{ 
     $files = $request->file('file'); 
     $no_of_files = count($files); 
     /*foreach ($files as $file)  //Don't know why this 'foreach' is not working 
     { 
     $file->getClientOriginalName(); 
     }*/ 
     if($no_of_files==0) 
     { //To check if any in-essential AJAX is called 
     return "Valueless Calling !!!!!!"; 
     } 
     for($i=0;$i<$no_of_files;$i++) 
     { 
     $file = $files[$i];//->getClientOriginalName(); 
     if($file) 
     { 
      $random_original_image_name=str_random(30).".".$file->getClientOriginalExtension(); 
      $file->move(public_path().'/uploads/userfiles/images/',Auth::id()."-".$random_original_image_name); 
      $original_image_name=base_path()."/public/uploads/userfiles/images/".Auth::id()."-".$random_original_image_name; 
      $thumb_image_name=base_path()."/public/uploads/userfiles/thumbs/".Auth::id()."-".$random_original_image_name; 
      if(file_exists($original_image_name)) 
      { 
       $thumb=Image::make($original_image_name)->resize(200,null,function($constraint){ 
        $constraint->aspectRatio(); 
       }); 
       $thumb->save($thumb_image_name); 

       $id=UserSlidePhoto::insertGetId(
                [ 
                'user_id'=>Auth::id(), 
                'social_image_id'=>0, 
                'original_image_name'=>Auth::id()."-".$random_original_image_name, 
                'thumbnail_name'=>Auth::id()."-".$random_original_image_name 
                ] 
               ); 
       $image_info=UserSlidePhoto::find($id); 
      } 
     } 
     else 
      return response()->json("ERROR",400); 
     } 
    return "SUCCESS"; 
} 
相關問題