2015-04-23 94 views
2

我已經爲我的ownCloud應用程序編寫了一個REST接口。我有方法getFileFromRemote($path)應該返回一個帶有文件內容的JSON對象。 不幸的是,這隻適用於我在$path中指定的文件是純文本文件。當我嘗試調用圖像或PDF的方法時,狀態碼爲200,但響應爲空。爲了返回文件內容,我使用file_get_contents來檢索內容。使用JSON發送二進制文件內容


注:我知道ownCloud有一個WebDAV的接口,但我想只用REST來解決這個問題。


EDIT 這是代碼服務器側(ownCloud):

public function synchroniseDown($path) 
{ 
    $this->_syncService->download(array($path));//get latest version 
    $content = file_get_contents($this->_homeFolder.urldecode($path)); 
    return new DataResponse(['path'=>$path, 'fileContent'=>$content]); 
} 

第一行檢索downloades的ownCloud服務器上的內容和工作原理完全。

+0

接收和發送內容的代碼很有幫助。 – yergo

+0

我已經添加了一些代碼 – user2810895

回答

2

你可能不得不base64_encode您的文件內容進行json_encode /解碼處理得當:

return new DataResponse([ 
    'path'=>$path, 
    'fileContent' => base64_encode($content) // convert binary data to alphanum 
]); 

,然後通過第二側接收文件時,你必須總是:

$fileContent = base64_decode($response['fileContent']); 

這只是一個,但最簡單的方法來處理。順便說一句,遲早你會發現,Mime-Type將是有用的迴應。