Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从php rest API下载图像并显示它_Php_Angularjs_Image - Fatal编程技术网

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

从php rest API下载图像并显示它,php,angularjs,image,Php,Angularjs,Image,我正在尝试使用一个函数从服务器获取图像,但没有错误,也没有显示任何内容。我花了很多时间搜索,但没有成功,我真的需要帮助,希望有人能够提供帮助 在后端方面,我有如下内容: header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="'.basename($fileP

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

在后端方面,我有如下内容:

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 );
}
head.profilPic= 'data:image/jpeg;base64,'+ data.data;
serviceAPI('GET','files')。然后(函数(数据){
head.profilPic='数据:图像/jpeg;base64',+_arrayBufferToBase64(数据);
},函数(数据){
});
函数_arrayBufferToBase64(缓冲区){
var二进制=“”;
var字节=新的Uint8Array(缓冲区);
var len=字节数。字节长度;
对于(变量i=0;i

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

我终于找到了解决办法

后端:

  $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;
控制器:

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 );
}
head.profilPic= 'data:image/jpeg;base64,'+ data.data;

如何从服务器获取图像?以url的形式还是以base64的形式?我使用带有$filepath的readfile(图像的url)。但我也尝试过不使用base64,但没有任何效果。我应该换什么?