0

On the client Side I am using Jquery to convert an Image from image to a Base64 String and sending it back to the server. On the server I am storing the String as it is and it is also rendering back on the required pages when i pit the image shows nicely. however when I use this class below I am not able to store the image on the disk on the server or use ImageIO.read or using output Stream it does not show up after wards.

public static void saveCompanyLogo(String path,String logo,String comId){
    String s[]=logo.split(";");
    String format=s[0].substring(11);
    File file = new File(path+"SecurePass\\logos\\");
    if (!file.exists()) {
        boolean result = false;

        try{
            File f=new File(path+"SecurePass\\logos\\");
            f.mkdir();
            result = true;
        } catch(SecurityException se){
            //handle it
        }        
        if(result) {    
        }
    }
    byte[] b=Base64.decodeBase64(logo.replaceAll(" ", "+"));
    try (OutputStream stream = new FileOutputStream(path+"SecurePass\\logos\\"+comId+"."+format)) {
        stream.write(b);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


}

I am using replaceAll on the suggestion of a Overflow user, but no go. Please help. In the above code "logo" contains the Base64 String. I have also used ImageIO.read using imagereaders but no go. Seems that something is wrong in the data.

Thanks.

Abhijeet
  • 689
  • 2
  • 10
  • 19

2 Answers2

0

you can do it easily by using canvas. Like this:

var img =document.getElementbyId('image');

function baseImage(img){ // img is the image object
var canvas = document.createElement("canvas");
                canvas.width = img.width;
                canvas.height = img.height;

                // Copy the image contents to the canvas
                var ctx = canvas.getContext("2d");
                ctx.drawImage(img, 0, 0, img.width, img.height);

                // Get the data-URL formatted image
                var dataURL = canvas.toDataURL("image/jpeg");

                return dataURL.replace("data:image/jpeg;base64,", "");
}
Rahul Munjal
  • 2,551
  • 2
  • 15
  • 35
  • You do not read the post nicely.. I am saying I have problems on server.. Above code is server side code.. I simply want to save the base64 String as an image... on the server... Why mark me down... I have no problem relating the client side..! – Abhijeet Dec 23 '14 at 11:15
0

WE have to strip off the String from the client string .. details on this post..

Convert base64 string to image

Abhijeet
  • 689
  • 2
  • 10
  • 19