1

I am using Html5, Java script, ajax and java. I am uploading a image from desktop to the crop and after the crop it is showing in bootstrap modal in same page. But i am not getting URL for this Image, I am getting some Base64 code and when i am sending this base64 code than it is not working.

I seen this post but i did not get any solution from this link: https://stackoverflow.com/

This code for static image, Showing first time.

My code:

HTML:

  <div class="img-container">
         <img src="../assets/img/picture.jpg" alt="Picture">
    </div>
<div class="modal fade docs-cropped" id="getCroppedCanvasModal" aria-hidden="true" aria-labelledby="getCroppedCanvasTitle" role="dialog" tabindex="-1">
 <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <a class="btn btn-primary" id="download"  download="cropped.png" href="javascript:void(0);">Upload</a>

 </div>
</div>

Java script Code:

     (function () {
            var $image = $('.img-container > img');
            var $download = $('#download');
        $('#getCroppedCanvasModal').modal().find('.modal-body').html(result);
                    if (!$download.hasClass('disabled')) {
                        $download.attr('href', result.toDataURL());
                        //console.log("*****************"+result.toDataURL());
                        var swapUrl = result.toDataURL();
                        console.log("*******" + swapUrl);

//                        document.getElementById('replaceMe').src = swapUrl;

                        $('#download').click(function () {
                            var b = result.toDataURL();
                            $.ajax({
                                url: "/sf/p/customizeText",
                                type: 'GET',
                                data: b,
                                success: function (response) {
                                    console.log("999999999999999999999999999999999----------------" + b)
                                },
                                complete: function (response) {

                                },
                                error: function (response) {

                                }
                            });
                        });
                    }
}

I am assign result.toDataURL() into variable b. But it is showing some base64 code. How i am send this image to server.

I am giving one snippet. enter image description here Please give me some idea achieve to this solution.

4
  • When you have the base64 encoded data of your image, you can re-create the image serverside. Just send your Base64 data with the rest of the data to the server and recreate it there using (if php) base64_decode(base64) and imagecreatefromstring(decoded_Base64_data) Commented Oct 21, 2015 at 6:02
  • What serverside language are you using .. PHP ? Commented Oct 21, 2015 at 6:02
  • I am using advance java Commented Oct 21, 2015 at 6:10
  • Does this previous thread help you with creating your image from base64 server side ? stackoverflow.com/questions/17506428/… .. First step anyways is to just receive the base64 encoded image string serverside, and from there re-create the image from that string .. Commented Oct 21, 2015 at 6:19

1 Answer 1

0

Hi you can check this solution also

Javascript code

    var base64before = document.querySelector('img').src;
    var base64 = base64before.replace(/^data:image\/(png|jpg);base64,/, "");
    var httpPost = new XMLHttpRequest();
    var path = "your url";
    var data = JSON.stringify(base64);

    httpPost.open("POST", path, false);
    // Set the content type of the request to json since that's what's being sent
    httpPost.setRequestHeader('Content-Type', 'application/json');
    httpPost.send(data);

This is my Java code.

    public void saveImage(InputStream imageStream){
    InputStream inStream = imageStream;

    try {
        String dataString = convertStreamToString(inStream);

        byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(dataString);
        BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes));
        // write the image to a file
        File outputfile = new File("/Users/paul/Desktop/testkey/myImage.png");
        ImageIO.write(image, "png", outputfile);

        }catch(Exception e) {
            System.out.println(e.getStackTrace());
        }
    }



  static String convertStreamToString(java.io.InputStream is) {
    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.