2014-12-06 120 views
0

當在一個簡單的Laravel 4測試應用創建視圖當用戶點擊「上傳」,jQuery的發送FORMDATA到控制器Laravel控制器無法使用jQuery

var formdata = new FormData(form); 
    request.open('post', 'submit'); 
    request.send(formdata); 

控制器接收文件並對陣列圖片。

foreach(Input::file("image") as $image) { 

     $imagename = $image->getClientOriginalname(); 
     $uploadflag = $image->move('public/uploads', $imagename); 
     if($uploadflag){ 

      $uploadedImages[] = $imagename; 
     } 
    } 
return Response::json(['success' => true, 'message' => 'images uploaded successfully', 'images' => $uploadedImages]); 

現在我想在視圖中顯示(show.php)的圖像,以便在控制器:

return View::make('show') 
        ->with('uploadedImages', $uploadedImages); 

show.php只包含一個循環:

@foreach($uploadedImages as $uploadedImage) 

     <div>{{ HTML::image($uploadedImage) }}</div> 

     @endforeach 

但沒有任何反應 - 視圖不顯示。 Laravel日誌文件中沒有錯誤消息,Response :: json結果顯示圖像在數組中。 以下是我已經嘗試了路線:

Route::post('show', function() 
{ 
    return View::make('show'); 
}); 

,我已經試過

Route::get('show', function() 
{ 
    return View::make('show'); 
}); 

,我已經試過

Route::post('show', array(
    'as' => 'show', 
    'uses' => '[email protected]' 
)); 

沒有jQuery的,沒有問題與控制器 - 返回View :: make - 但現在jQuery處理上傳不起作用。我需要jQuery來允許用戶對要上傳的文件進行最終選擇。 任何和所有的幫助,最歡迎!

回答

0

如果你的輸出文件只是「show.php」,它將不起作用,文件必須具有擴展名.blade,就像這個「show.blade.php」一樣。

,不要忘了這個文件添加到您的「路線」了。

+0

嗨,我改成了show.blade.php,但並不路線::讓您的視圖文件( 'show',function(){return View :: make('show');));好?它仍然不起作用。 – metarzan 2014-12-06 21:45:17

+0

僅在輸出結果前才返回視圖 – 2014-12-07 04:09:51

+0

謝謝,但沒有變化。響應頭顯示200 OK,Chrome Canary DevTools預覽顯示正確放置在頁面上的圖像細節,但頁面無法加載。我想張貼一張圖片來展示給你,但我需要首先獲得10的聲望。在我看來,如果圖像在頁面代碼中,它必定是Laravel中我看不到的東西。 – metarzan 2014-12-08 02:38:24

0

使用字符串函數像以前一樣爲

return (String) view('Company.allUserAjax',$data) 

後個夠將響應