php - 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));
}
//...
- guestIDCard() 以
header('Content-type: image/jpeg');
- 如果我尝试保存图像,它的作品。(
imagejpeg($img, "./save/here/or/here");
)但我试图将它带到浏览器而不保存它
那么,当我使用从表单获得的 $data 成功创建图像时,我如何将自己重定向到 guestprint.php?
解决方案
一种方法是将服务器上的图像转换为 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)
推荐阅读
- javascript - 如何使用 React Native 本地化更改语言?
- react-native - 在 Postman 上将图像上传到 S3 有效,但是当我使用 React Native Image Picker 时它不起作用
- javascript - ng-init 最初没有获取范围值
- dart - 登录/注册页面
- javascript - jquery平滑滚动不正确的结果
- r - 相互反应的闪亮 Flexdashboard 过滤器
- javascript - 如何将转录语音推送到文本结果到 textarea
- c# - CKEditor:无法通过拖放上传图像
- excel - 在 VBA 中使用不包括周末(星期五)的日期加法
- sql - 通过子查询中的 id 列表选择条目?