我正在尝试使用成功函数打开guestprint.php
(使用输入生成图像)的结果。
Controller.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) {
---???---
});
}
guestprint.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));
}
//...
header('Content-type: image/jpeg');
imagejpeg($img,
"./save/here/or/here");
)但我想把它带到浏览器
没有保存它那么当我从表格中获得$ data成功创建图像时,如何将自己重定向到guestprint.php?
答案 0 :(得分:0)
一种方法是将服务器上的图像转换为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}}">
p.s。:.success()和.error()已弃用(1.4+)并在(1.6+)中完全删除。我建议使用.then(成功,错误)
答案 1 :(得分:0)
我已经厌倦了,并且保存了图像。
Controller.js:
$scope.guestPrint = function() {
$http({
url: request_server + 'guestprint.php',
method: 'POST',
data: $scope.admin, //dont mind this
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).success(function(response, status, headers, config) {
$http({
url: request_server + 'guestdw.php',
method: 'POST',
data: { user: $rootScope.user }, //dont mind this
responseType: 'arraybuffer',
headers: {
'Content-Type': 'application/x-www-form-urlencoded' //!important
}
}).success(function(response, status, headers, config) {
var blob = new Blob([response], {
type: "image/jpeg"
});
var objectUrl = URL.createObjectURL(blob);
window.open(objectUrl);
});
});
}
guestdw.php:
$yourfile = "./path/to/file.jpeg";
$file_name = basename($yourfile);
header('Content-Description: File Transfer');
header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename="'.($file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($yourfile));
readfile($yourfile);