以angularJS显示base64图像

时间:2016-08-19 13:11:38

标签: java angularjs

我已使用java代码

将Base64图像插入数据库
        FileInputStream mFileInputStream = new FileInputStream("C:\\basicsworkspace\\base64upload\\src\\main\\resources\\basic.png");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int bytesRead = 0;
        while ((bytesRead = mFileInputStream.read(b)) != -1) {
           bos.write(b, 0, bytesRead);
        }
        byte[] ba = bos.toByteArray();
        //byte[] encoded = Base64.getEncoder().encode(ba); // util
        byte[] encoded = Base64.encodeBase64(ba); //apache
        connection = DriverManager.getConnection(connectionString);  
         String insertSql = "INSERT INTO test (image) VALUES (?)" ; 
              prepsInsertProduct.setBytes(encoded);
         System.out.println(insertSql);
        prepsInsertProduct = connection.prepareStatement(  
            insertSql);  
         System.out.println(prepsInsertProduct.execute());  

但是如果尝试使用角度js显示图像则不显示图像。在sqlserver中,我将图像保存为varbinary(max)和角度代码,

      config(function ($compileProvider) {
             console.log($compileProvider.imgSrcSanitizationWhitelist());
        })

     <img src="choice:image/png;base64,{{choice.Icon}}"> 

但我只收到带有这样一条消息的字节,

 unsafe:choice:image/png;base64,6956424F5277304B47676F414141414E535568455567…
Please let me know where I am going wrong and the image is in PNG format.

2 个答案:

答案 0 :(得分:0)

在POJO中我对byte []进行了更改并且它有效。我有String的数据类型并修改为byte []但是没有显示Base64图像,但是t只显示字节数组图像。

答案 1 :(得分:0)

试试你的控制器:

$scope.choice.Icon = 'data:image/jpeg;base64,' + yourImageData;

在你的HTML中:

<img ng-src="{{choice.Icon}}">