简体   繁体   English

Angularjs,$ http.get在图像数据上给出特殊字符

[英]Angularjs, $http.get on image data give special characters

I use a private API using PHP and slim framework. 我使用使用PHP和slim框架的私有API。 When I get a route which gives me an image datas the response seems to be strange (like charset issue). 当我得到一条为我提供图像数据的路线时,响应似乎很奇怪(例如字符集问题)。

The server send the image with readfile() php function: 服务器使用readfile()php函数发送图像:

$src = "image.png";
$this->app->contentType("image/png");
$this->app->response()->status(REQ_OK);
header('Content-length: '.filesize(SITE_PATH.$src));
readfile(SITE_PATH.$src);

On client side I use AngularJS with $http. 在客户端,我将AngularJS与$ http一起使用。

$http({
      method: 'GET',
      url: "/myRoute",
      headers: {
               'responseType': "image/png"
      }
)
.success(function(result){
   //treatment
})
.error(function(){

});

On chrome, in the xhr preview i've got this: ![enter image description here][1] 在chrome上,在xhr预览中,我得到了这一点:![在此处输入图片描述] [1]

See the screenshot here : http://i.stack.imgur.com/G5Cna.png 在此处查看屏幕截图http : //i.stack.imgur.com/G5Cna.png

if I send data to ng-src of img it doesn't work. 如果我将数据发送到img的ng-src,它将无法正常工作。 Because of the weird encode. 由于编码怪异。

Have you any idea? 你有什么主意吗

Thank you very much 非常感谢你

The problem is that you are trying to insert a binary image into the HTML source code which is not possible. 问题是您试图将二进制图像插入HTML源代码中是不可能的。

You will either need to form an IMG data URL (see eg this question for an example: AngularJS - Show byte array content as image ) 您将要么需要形成一个IMG数据URL(例如,请参见此问题的示例: AngularJS-将字节数组内容显示为image

Or you need to let the browser do the img loading for you. 或者,您需要让浏览器为您执行img加载。 That would mean creating an <img ng-src="{{imgPath}}" /> where imgPath refers to a variable with content /myRoute . 这将意味着创建一个<img ng-src="{{imgPath}}" /> ,其中imgPath指向内容为/myRoute imgPath的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM