从php rest API角度下载图像并显示它

时间:2016-02-10 00:10:53

标签: php angularjs image

我正在尝试使用一个函数从服务器获取图像,但没有错误,也没有出现。我花了很多时间搜索没有成功,我真的需要帮助,希望有人能够。

在后端,我有类似的东西:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment;  filename="'.basename($filePath).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filePath));
readfile($filePath);

HTML:

<img class="user_pic img-circle" ng-src="{{head.profilPic}}">

控制器:

serviceAPI('GET','files').then(function(data) {
  head.profilPic= 'data:image/jpeg;base64,'+_arrayBufferToBase64(data);
}, function(data) {
});

function _arrayBufferToBase64( buffer ) {
  var binary = '';
  var bytes = new Uint8Array( buffer );
  var len = bytes.byteLength;
  for (var i = 0; i < len; i++) {
    binary += String.fromCharCode( bytes[ i ] );
  }
  return window.btoa( binary );
}

等待你的建议。提前谢谢。

1 个答案:

答案 0 :(得分:0)

最后我找到了解决方案。

后端:

  $img_src = $filePath;
  $imgbinary = fread(fopen($img_src, "r"), filesize($img_src));
  $img_str = base64_encode($imgbinary);

  header("Content-Type: text/plain");
  echo $img_str;

控制器:

head.profilPic= 'data:image/jpeg;base64,'+ data.data;
相关问题