从angularJS获取php base64图像
我试图从angularJS获取一个php中的64base编码字符串,但我不太确定我做错了什么。我对一个图像进行了编码,该图像是由php运行的makegray.exe写入服务器的。现在,我试图抓取该图像并将其返回给angularJS,然后由angularJS为用户显示该图像。这似乎是一个很简单的问题,但我不知道如何做到这一点从angularJS获取php base64图像,php,angularjs,base64,Php,Angularjs,Base64,我试图从angularJS获取一个php中的64base编码字符串,但我不太确定我做错了什么。我对一个图像进行了编码,该图像是由php运行的makegray.exe写入服务器的。现在,我试图抓取该图像并将其返回给angularJS,然后由angularJS为用户显示该图像。这似乎是一个很简单的问题,但我不知道如何做到这一点 $scope.MakeGray_Button = function(){ if ($scope.imageUrl) { var MakeGray_Fo
$scope.MakeGray_Button = function(){
if ($scope.imageUrl) {
var MakeGray_Form = new FormData();
MakeGray_Form.append("FileName", $scope.imageUrl);
$http({
method : "POST",
url : "../opencv/MakeGray/MakeGray.php",
data : MakeGray_Form,
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
}).
success(function(){
// some magic code here that grabs the $base64 variable from php
})
.error(function(){});
}
else{
alert("Please upload an image");
}
}
php
首先,使用.then()
代替您不推荐使用的函数.success()
和.error()
然后也像上面的代码一样捕获响应
通过将base64图像设置为img
标记的src
,可以轻松渲染该图像<代码>ng src应该对此有所帮助
如果你想让图像成为字符串,你应该检查响应对象。好的,我让它工作了
$scope.MakeGray_Button = function(){
if ($scope.imageUrl) {
var MakeGray_Form = new FormData();
MakeGray_Form.append("FileName", $scope.imageUrl);
$http({
method : "POST",
url : "../opencv/MakeGray/MakeGray.php",
data : MakeGray_Form,
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
}).
success(function(data){
$scope.data = data;
base64 = data;
$("#img2").prop("src", base64);
})
.error(function(){});
}
else{
alert("Please upload an image");
}
}
希望这能帮助其他试图使用base64编码字符串从php向JS显示图像的人 你犯了什么错误?检查Chrome中的控制台,看看您从请求中得到了什么响应。哦,我应该提到我没有收到任何错误,只是我不知道如何获取php中的$base64字符串。我已经在stackoverflow上找到了一些解决方案,但没有一个能奏效。这是firebug对我说的success(function(response){console.log(response)})
试试这个,让我们知道你得到了什么样的响应??好的,我会试试这个,然后报告回来它说它在firebug中是(一个空字符串)。当我尝试alert(response)时,这个响应显示为空。好的,别介意,我发现它确实响应base64,但它没有显示。出于某种原因,base64字符串周围有一对额外的引号。。。
$scope.MakeGray_Button = function(){
if ($scope.imageUrl) {
var MakeGray_Form = new FormData();
MakeGray_Form.append("FileName", $scope.imageUrl);
$http({
method : "POST",
url : "../opencv/MakeGray/MakeGray.php",
data : MakeGray_Form,
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
}).then(function(response){
//response object should hold your base64 image data.
//you can change src of an img tag with ng-src and let the browser render your image.
},function(response){});
}
else{
alert("Please upload an image");
}
}
$scope.MakeGray_Button = function(){
if ($scope.imageUrl) {
var MakeGray_Form = new FormData();
MakeGray_Form.append("FileName", $scope.imageUrl);
$http({
method : "POST",
url : "../opencv/MakeGray/MakeGray.php",
data : MakeGray_Form,
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
}).
success(function(data){
$scope.data = data;
base64 = data;
$("#img2").prop("src", base64);
})
.error(function(){});
}
else{
alert("Please upload an image");
}
}