首页 > 解决方案 > Angularjs .success{} 与 php 生成的图像

问题描述

我正在尝试guestprint.php使用成功功能打开(生成带有输入的图像)的结果。

控制器.js:

$scope.guestPrint = function() {

 $http({
   url: request_server + 'guestprint.php',
   method: 'POST',
   data: $scope.admin,
   headers: {
   'Content-Type': 'application/x-www-form-urlencoded'
   }

 }).success(function(response, status, headers, config) {
                     ---???---
 });

}

来宾打印.php:

<?php
header('Access-Control-Allow-Origin: *');
header("content-type: application/json");

$data = json_decode(file_get_contents("php://input"));

//...
//..database and authorisation stuff
//...

if($result['result'] == 1 && $user_role == 1 ) {
    $ModelCertificate=new ModelCertificate();
    echo json_encode($ModelCertificate->guestIDCard($data));
}
//...

那么,当我使用从表单获得的 $data 成功创建图像时,我如何将自己重定向到 guestprint.php?

标签: phpangularjs

解决方案


一种方法是将服务器上的图像转换为 base64 字符串。

$data = file_get_contents(<PATH TO IMG>);
$base64 = 'data:image/<IMAGE EXTENSION (png, jpg, etc..)>;base64,' . base64_encode($data);

将该 base64 字符串发送回前端。

一旦你把它放在前端,你就可以使用那个 base64 字符串作为 img 的 src。

<img ng-src="{{base64img}}">

ps: .success() 和 .error() 已弃用 (1.4+) 并在 (1.6+) 中完全删除。我建议使用 .then(success, error)


推荐阅读