php - Sending binary file content using JSON -
i've written rest interface owncloud app. i've method getfilefromremote($path)
should return json object file content. unfortunately works when file i've specified in $path
plaintext file. when try call method image or pdf
status code 200 response empty. returning file contents use file_get_contents
retrieving content.
note: know owncloud has webdav interface, want solve rest only.
edit code server side (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]); }
the first line retrieves downloades content on owncloud server , works completely.
you have base64_encode
file content make json_encode/decode handle properly:
return new dataresponse([ 'path'=>$path, 'filecontent' => base64_encode($content) // convert binary data alphanum ]);
and when receiving file via second side, have always:
$filecontent = base64_decode($response['filecontent']);
it's one, ones of easiest way handle that. btw, sooner or later find mime-type
useful in response.