Javascript 将字节数组显示为角度js中的图像
我在数据库中有一个图像。我想检索图像并使用angular js将其显示在网页中。在获取数据之后,我有一个字节数组。如何将数据发送到html页面。我对以下代码有问题。。请帮忙。Javascript 将字节数组显示为角度js中的图像,javascript,java,angularjs,servlets,Javascript,Java,Angularjs,Servlets,我在数据库中有一个图像。我想检索图像并使用angular js将其显示在网页中。在获取数据之后,我有一个字节数组。如何将数据发送到html页面。我对以下代码有问题。。请帮忙。 当我单击链接查看图像时,页面将向服务器发送两个get请求,而不是一个。它连续发送请求2次。 注意:我已尝试使用以下链接。。但它不起作用 下面是我的js文件 app.controller('aboutCtrl', function($scope,$http,$location) { $scope.message = 'Th
当我单击链接查看图像时,页面将向服务器发送两个get请求,而不是一个。它连续发送请求2次。 注意:我已尝试使用以下链接。。但它不起作用 下面是我的js文件
app.controller('aboutCtrl', function($scope,$http,$location) {
$scope.message = 'This is Add new order screen';
var url = '/Angular/login';
$http.get(url).success(function(result) {
$scope.image = result.image;
})
});
//html
<img data-ng-src="data:image/PNG;base64,{{image}}">
Servlet代码
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("inside get");
JSONObject result= new JSONObject();
Retreive rt = new Retreive();
result.put("image", rt.getImage());
response.setContentType("application/json");
PrintWriter out = response.getWriter();
out.write(result.toString());
out.flush();
out.close();
}
似乎您的
图像
不是base64编码字符串,而是字节数组。您需要先对其进行编码,才能像这样使用它
检查文档
您也可以尝试这种方法您可以提供正在单击的
元素
的代码吗?图像真的是基于64编码的吗?如果没有,那么它就不能工作
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("inside get");
JSONObject result= new JSONObject();
Retreive rt = new Retreive();
result.put("image", rt.getImage());
response.setContentType("application/json");
PrintWriter out = response.getWriter();
out.write(result.toString());
out.flush();
out.close();
}