2017-03-25 31 views
0

我想設置圖像路徑與控制器,因爲我選擇添加我的圖像上傳出web /目錄。Symfony 406(Not Acceptable)

我創建ServeController:

public function urlAction($filename) 
 
\t { 
 
\t  $path = "../data/uploads/admin/"; 
 

 
\t  $response = new BinaryFileResponse($path.$filename); 
 
     $response->headers->set('Content-Type', 'image/jpeg'); 
 
     $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_INLINE); 
 

 
     return $response; 
 
\t  
 
\t }

這項工作從我的前端reactJs,但我試圖在我的symfony後臺顯示圖像我有錯誤406不可接受

在我的樹枝模板中,我使用{{url('url_image',{'filename':data.filename})}}

有什麼想法?

回答

0

你可以試試這個:

如果
public function urlAction($filename) 
{ 
    $path = "../data/uploads/admin/"; 

    $response = new Response(); 
    $disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE, $filename); 
    $response->headers->set('Content-Disposition', $disposition); 
    $response->headers->set('Content-Type', 'image/jpeg'); 
    $response->setContent(file_get_contents($path)); 

    return $response; 
} 

不知道,但會解決它。請嘗試。

+0

我爲API && 1爲symfony後臺創建了2條單獨的路徑。但我仍然有一個小問題要解決,當我使用url()裏面的一個循環樹枝圖像不顯示 – Nicks

+0

嗨尼克斯。聽起來像是一個不同的問題。你能發表一個新的問題嗎? –